Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Simples asp .net web api project, default controller, simplest possible POST handler:
[HttpPost]
        public void Post([FromBody]string value)
        {
            int a = 0;
        }


Sending simple post request with Postman to:
http://localhost:port/api/values


I can only send raw data of type application/json - sending anything else results in controller action not getting executed at all, but no matter what I am sending,
value
in the handler is always null. Can someone point out what I am doing wrong?

What I have tried:

I have tried, like, everything that I'm aware of.
Posted
Updated 23-Feb-18 4:09am
v2

Ok, figured out it myself. To send a simplest possible string, it has to be wrapped in quotes. How obvious was it.
 
Share this answer
 
v2
By default, WebAPI only binds to JSON or XML requests (or the formats supported by whatever other media formatters you have configured). Wrapping the string in quotes will effectively turn it into a JSON payload, but you'll need to watch out for encoding issues, particularly if your string already contains quotes.

If you just want to capture the raw request body, there are other ways to do that:
Accepting Raw Request Body Content with ASP.NET Web API - Rick Strahl's Web Log[^]

The simplest option is probably:
C#
[HttpPost]
public async Task Post()
{
    string rawRequestBody = await Request.Content.ReadAsStringAsync();
    ...
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900