3

I have a

public void Post([FromBody]Record value) 

-method that I can call from fiddler with some json.

When calling the method from my mac using WizTools RESTClient 3.1 with the same json, the value is always null. It looks like it does not get parsed or something.

I'm using Content-Type: application/json on both machines and I have double-checked that the Request.Content object the headers in the Visual Studio debugger.

If I use a simpler object with only 2 properties like so:

public class Test123 { public string name { get; set; } public int age { get; set; } } public void Post([FromBody]Test123 value) 

I can call it from both fiddler and the mac and the value is never null.

So any tips on how to debug this? Is there for instance any way for me to see the raw response sent from my mac to iis/visual studio on my PC? It does not show up in fiddler.

1
  • On a Mac, you can use Charles Proxy for debugging http (equivalent to Fiddler) Commented Jul 12, 2013 at 11:37

3 Answers 3

7

For your action method, if you send a request with Content-Type: application/json and the request message body of {"name":"john", "age":20}, it should correctly bind. BTW, you do not need to use [FromBody], since Test123 is a complex type.

Anyways, you can view the raw request message by adding a message handler like this.

public class MyHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken token) { HttpMessageContent requestContent = new HttpMessageContent(request); string requestMessage = requestContent.ReadAsStringAsync().Result; return await base.SendAsync(request, token); } } 

Add the handler like this config.MessageHandlers.Add(new MyHandler()); in WebApiConfig.cs Register method. Raw request will be in requestMessage variable. You can inspect by breaking there while debugging or write to a trace, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

Just add request.Content = new StringContent(requestMessage.Substring(requestMessage.IndexOf("{")), Encoding.UTF8, "application/json"); so as to not interfere with the proceeding chain.
0

I think you must inform the content-type on the http header request. For sample, if you want to pass a json format, in the header you have to add:

Content-Type: application/json

And in the Body:

{ name: "John", Age: 20 } 

You will get the Test123 object create by the model binder of asp.net mvc web api.

I like to use Fiddler software to make requests manually, and you also can see the raw request/response, create requests with any http verb, etc.

Comments

0

There is a feature called "Failed Request Tracing" on IIS (not IIS Express or Kestrel). This feature will show you the detailed logs for any request/response cycle that passes your filter criteria.

Here is what the output log looks like:

enter image description here

To Enable this feature:

  1. Start -> Run -> type in "inetmgr" [ this will open up the IIS management UI]
  2. At the server level Enable FTW and add rules enter image description here
  3. Add a rule ( you can add HTTP 200 code as filter to trace successful request, or some other code - as you see fit)enter image description here
  4. Last step - I promise - Each site will need to "opt-in" to this featureenter image description here

Thats it.. now do your thing and go to the folder with the logs, sort by time. You should see a new log entry that was created around the time of your testing.

Good Luck!!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.