How to compose request for REST web method in fiddler

Sunny picture Sunny · Mar 14, 2013 · Viewed 55.7k times · Source

I am able to call web serivce but name property is not binding.

Fiddler request

POST http://localhost:50399/api/custservice/ HTTP/1.1
User-Agent: Fiddler
Host: localhost: 50399
Content-Length: 28
{ "request": { "name":"test"}}

POST Webmethod

public string Any(CustomerRequest request)
{
  //return details
}

CustomerRequest.cs

public class CustomerRequest 
{
  public string name {get;set;}
}

Answer

Jon Susiak picture Jon Susiak · Mar 14, 2013

First of all you need to add Content-Type 'application/json' to the request:

POST http://localhost:50399/api/custservice/ HTTP/1.1
User-Agent: Fiddler
Host: localhost: 50399
Content-Type: application/json

Then change your POST data to:

{"name":"test"}

You will be able to access the data using:

public string Any(CustomerRequest request)
{
  return request.name
}

Alternatively using your existing POST data structure create a new class:

public class RequestWrapper
{
  public CustomerRequest request { get; set; }
}

and change your Action method to:

public string Any(RequestWrapper wrapper)
{
  return wrapper.request.name;
}