Post parameter is always null
Since you have only one parameter, you could try decorating it with the [FromBody]
attribute, or change the method to accept a DTO with value as a property, as I suggested here: MVC4 RC WebApi parameter binding
UPDATE: The official ASP.NET site was updated today with an excellent explanation: https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1
In a nutshell, when sending a single simple type in the body, send just the value prefixed with an equal sign (=), e.g. body:
=test
I've just had this occur using Fiddler. The problem was that I hadn't specified Content-Type
.
Try including a header for Content-Type
in your POST request.
Content-Type: application/x-www-form-urlencoded
Alternatively, as per comments below, you may need to include a JSON header
Content-Type: application/json
I have been scratching my head over this today.
My solution is to change the [FromBody]
to a HttpRequestMessage
, essentially moving up the HTTP stack.
In my case I am sending data across the wire which is zipped json which is then base64'd. All this from an android app.
The original signature of my web endpoint looked like this (using [FromBody]
) :
My fix for this issue was to revert to using a HttpRequestMessage
for the signature of my endpoint.
You can then get access to the post data using this line of code:
This works and allows you access to the raw untouched post data. You don't have to mess around with fiddler putting an = sign at the beginning of your string or changing the content-type.
As an aside, I first tried to following one of the answers above which was to change the content type to: "Content-Type: application/x-www-form-urlencoded". For raw data this is bad advice because it strips out + characters.
So a base64 string that starts like this: "MQ0AAB+LCAAAAAA" ends up like this "MQ0AAB LCAAAAAA"! Not what you want.
Another benefit of using HttpRequestMessage
is that you get access to all the http headers from within your endpoint.