Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view the request body in ASP.NET Web API from Visual Studio?

Tags:

How can I view the request body in ASP.NET Web API from Visual Studio? In ASP.NET MVC, you can use QuickWatch to inspect the Request object and view the content of the body and any posted form data. From what I read, ASP.NET Web API doesn't allow you to read the body more than once.

This is very annoying to deal with when trying to figure out why a specific value wasn't bound correctly. Is there a quick way to do this without setting up tracing/logging?

like image 535
Omar Avatar asked May 20 '13 14:05

Omar


People also ask

How do I pass body parameters in Web API?

Use [FromUri] attribute to force Web API to get the value of complex type from the query string and [FromBody] attribute to get the value of primitive type from the request body, opposite to the default rules.


Video Answer


1 Answers

The easiest it to install Fiddler. Then you will see everything that gets sent over the wire and inspect not only the request payload but the HTTP headers as well. And if you are consuming the API from javascript, things like FireBug, Chrome Developer Toolbar and IE Developer Tools will show you all network requests made by the website.

If you absolutely must inspect the traffic on the server then if you are hosting your Web API inside an ASP.NET application you could put the following line in your immediate window:

new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd() 
like image 65
Darin Dimitrov Avatar answered Oct 01 '22 01:10

Darin Dimitrov