Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET WebApi see raw request

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.

like image 524
Cotten Avatar asked Jul 23 '26 14:07

Cotten


1 Answers

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.

like image 154
Badri Avatar answered Jul 28 '26 16:07

Badri