Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post parameter is always null

People also ask

How do I pass a null parameter in Web API?

Simply add parameterName = null in your route parameter. Another option is add an overload. Have 2 function names receive different parameters. @kanika it's a precaution because there might be something yet to be setup in your controller and your controller is not accepting the parameters being sent.

What is FromBody?

Using [FromBody] When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object). At most one parameter is allowed to read from the message body.

Where is the FromBody attribute applied?

The [FromBody] attribute can be applied on only one primitive parameter of an action method. It cannot be applied to multiple primitive parameters of the same action method.


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 original endpoint

My fix for this issue was to revert to using a HttpRequestMessage for the signature of my endpoint.

enter image description here

You can then get access to the post data using this line of code:

enter image description here

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.


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've ran into this problem as well, and this is how I solved my problem

webapi code:

public void Post([FromBody] dynamic data)
{
    string value = data.value;
    /* do stuff */
}

client code:

$.post( "webapi/address", { value: "some value" } );