How to compose request for REST web method in fidd

2019-02-16 07:19发布

问题:

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;}
}

回答1:

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;
}