Since upgrading to RC for WebAPI I'm having some real odd issue when calling POST on my WebAPI. I've even gone back to the basic version generated on new project. So:
public void Post(string value)
{
}
and calling from Fiddler:
Header:
User-Agent: Fiddler
Host: localhost:60725
Content-Type: application/json
Content-Length: 29
Body:
{
"value": "test"
}
When I debug, the string "value" is never being assigned to. It's just always NULL. Anyone having this issue?
(I first saw the issue with a more complex type)
The problem is not only bound to ASP.NET MVC 4, the same problem occurs for a fresh ASP.NET MVC 3 project after RC installation
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.