locked
How to test my web api post action with fiddler RRS feed

  • Question

  • User264732274 posted

    i know how to test web api get action with fiddler but for post method i guess i need to send json. suppose there are few post action in my web api which insert many customer or update / delete a single customer. in this scenario i need to send json text from my fiddler client and i am not being able to generate json data in hand. so please see my code and tell me what json i need to pass for inserting multiple customer and update or delete a single customer. here i am posting my code. see my web api controller and customer class

    customer class
    ------------------
    
        public class Customer
        {
            public string CustomerID { get; set; }
            public string CompanyName { get; set; }
            public string ContactName { get; set; }
            public string ContactTitle { get; set; }
    
            public string Address { get; set; }
    
            public string Region { get; set; }
    
            public string PostalCode { get; set; }
            public string City { get; set; }
            public string Country { get; set; }
            public string Phone { get; set; }
            public string Fax { get; set; }
        }
    
        Web api controller
    ---------------------------
    
        [RoutePrefix("api/customer")]
        public class CustomerController : ApiController
        {
            static readonly ICustomerRepository repository = new CustomerRepository();
    
            [HttpGet, Route("GetAll")]
            public IEnumerable<Customer> GetAllCustomers()
            {
                return repository.GetAll();
            }
    
            [HttpGet, Route("GetByID/{customerID}")]
            public Customer GetCustomer(string customerID)
            {
                Customer customer = repository.Get(customerID);
                if (customer == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                return customer;
            }
    
            [HttpGet, Route("GetByCountryName/{country}")]
            public IEnumerable<Customer> GetCustomersByCountry(string country)
            {
                return repository.GetAll().Where(
                    c => string.Equals(c.Country, country, StringComparison.OrdinalIgnoreCase));
            }
    
            public HttpResponseMessage PostCustomer(Customer customer)
            {
                customer = repository.Add(customer);
                var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
    
                string uri = Url.Link("DefaultApi", new { customerID = customer.CustomerID });
                response.Headers.Location = new Uri(uri);
                return response;
            }
    
            public void PutProduct(string customerID, Customer customer)
            {
                customer.CustomerID = customerID;
                if (!repository.Update(customer))
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
    
            public void DeleteProduct(string customerID)
            {
                Customer customer = repository.Get(customerID);
                if (customer == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                repository.Remove(customerID);
            }
        }

    now tell give json for different post action. thanks

    Friday, August 5, 2016 11:43 AM

Answers

  • User36583972 posted

    Hi sudip_inn,

    You can refer the following suggestions.

    1: Use StreamWriter

              HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
                request.Method = "POST";
                request.KeepAlive = true;
                request.ContentType = "application/json; charset=utf-8";     
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    string PostData = new JavaScriptSerializer().Serialize(new Customer
                    {
                        CustomerID = "001",
                        CompanyName = "b",
                        ContactName = "hell",
                        ContactTitle = "qwq"
                        //.................
                    });
                    writer.Write(PostData);
                }
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string myResponse = "";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    myResponse = sr.ReadToEnd();
                }

    2: Adding a simple Test Client to ASP.NET Web API Help Page

    1) Creating Help Pages for ASP.NET Web API:

    http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages

    2) Adding a simple Test Client to ASP.NET Web API Help Page:

    https://blogs.msdn.microsoft.com/yaohuang1/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page/

    3: HttpClient

          var baseUrl = "url";
                HttpClient client = new HttpClient();
                Uri baseAddress = new Uri(baseUrl);
                client.BaseAddress = baseAddress;
                HttpResponseMessage response = client.PostAsJsonAsync("api/Values", new Customer).Result;
                //get states code
                int responsestate = (int)response.StatusCode;
    

    Finally, I suggest that you choose the right way, and try to make a test on your side.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 3:13 AM

All replies

  • User-782232518 posted

    You can simply write a .NET client for testing, http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client and once you capture the HTTP packets it sends, you can easily see how the JSON serialization is done.

    Sunday, August 7, 2016 6:31 AM
  • User36583972 posted

    Hi sudip_inn,

    You can refer the following suggestions.

    1: Use StreamWriter

              HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
                request.Method = "POST";
                request.KeepAlive = true;
                request.ContentType = "application/json; charset=utf-8";     
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    string PostData = new JavaScriptSerializer().Serialize(new Customer
                    {
                        CustomerID = "001",
                        CompanyName = "b",
                        ContactName = "hell",
                        ContactTitle = "qwq"
                        //.................
                    });
                    writer.Write(PostData);
                }
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string myResponse = "";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    myResponse = sr.ReadToEnd();
                }

    2: Adding a simple Test Client to ASP.NET Web API Help Page

    1) Creating Help Pages for ASP.NET Web API:

    http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages

    2) Adding a simple Test Client to ASP.NET Web API Help Page:

    https://blogs.msdn.microsoft.com/yaohuang1/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page/

    3: HttpClient

          var baseUrl = "url";
                HttpClient client = new HttpClient();
                Uri baseAddress = new Uri(baseUrl);
                client.BaseAddress = baseAddress;
                HttpResponseMessage response = client.PostAsJsonAsync("api/Values", new Customer).Result;
                //get states code
                int responsestate = (int)response.StatusCode;
    

    Finally, I suggest that you choose the right way, and try to make a test on your side.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 3:13 AM