By Robert Prine

2016-11-28 20:48:04 8 Comments

This is probably something very basic, but I am having trouble figuring out where I am going wrong.

I am trying to grab a string from the body of a POST, but "jsonString" only shows as null. I also want to avoid using a model, but maybe this isn't possible. The piece of code that I am hitting with PostMan is this chunk:

public void Test(int id, [FromBody] string jsonString)

Maybe it is something I am doing incorrectly with postman, but I have been trying to use "=test" (as seen in other questions asked about this topic) in the value section of the body - x-www-form-urlencoded section with the key as jsonString and nothing. I have also tried using raw - text and raw - text/plain. I get the id so I know the url is correct. Any help with this would be greatly appreciated.

PostMan is set up like this currently:

POST http://localhost:8000/Edit/Test?id=111
key = id  value = 111
Body - x-www-form-urlencoded
key = jsonString  value = "=test"


@Mohammad Alqerm 2019-09-05 12:51:48

Post the string with raw JSON, and do not forget the double quotation marks!

enter image description here

@Kudakwashe Mafutah 2019-06-24 09:16:23

After a long nightmare of fiddling with Google and trying out the wrong code in Stack Overflow I discovered changing ([FromBody] string model) to ([FromBody] object model) does wonders please not i am using .NET 4.0 yes yes i know it s old but ...

@Manohar Reddy Poreddy 2018-08-24 11:12:04

Finally got it working after 1 hour struggle.

This will remove null issue, also gets the JSON key1's value of value1, in a generic way (no model binding), .

For a new WebApi 2 application example:

Postman (looks exactly, like below):

POST    http://localhost:61402/api/values   [Send]

                   (*) raw             JSON (application/json) v

"{  \"key1\": \"value1\" }"

The port 61402 or url /api/values above, may be different for you.


using Newtonsoft.Json;
// ..

// POST api/values
public object Post([FromBody]string jsonString)
    // add reference to Newtonsoft.Json
    //  using Newtonsoft.Json;

    // jsonString to myJsonObj
    var myJsonObj = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(jsonString);

    // value1 is myJsonObj[key1]
    var valueOfkey1 = myJsonObj["key1"];

    return myJsonObj;

All good for now, not sure if model binding to a class is required if I have sub keys, or, may be DeserializeObject on sub key will work.

@Eugene 2018-06-01 08:36:05

In my case I forgot to use JSON.stringify(bodyStuff).

@Khizar Iqbal 2019-05-02 12:00:36

you saved my life buddy.

@ragnarswanson 2019-05-22 15:34:10

this is what solved my issues, thanks!

@Some random IT boy 2018-05-01 17:57:09

I know this answer is kinda old and there are some very good answers who already solve the problem.

In order to expand the issue I'd like to mention one more thing that has driven me crazy for the last 4 or 5 hours.

It is VERY VERY VERY important that your properties in your model class have the set attribute enabled.

This WILL NOT work (parameter still null):

/* Action code */
public Weird NOURLAuthenticate([FromBody] Weird form) {
    return form;
/* Model class code */
public class Weird {
    public string UserId {get;}
    public string UserPwd {get;}

This WILL work:

/* Action code */
public Weird NOURLAuthenticate([FromBody] Weird form) {
    return form;
/* Model class code */
public class Weird {
    public string UserId {get; set;}
    public string UserPwd {get; set;}

@Michael Freidgeim 2018-01-10 09:20:34

When having [FromBody]attribute, the string sent should not be a raw string, but rather a JSON string as it includes the wrapping quotes:


Based on

Similar answer string value is Empty when using FromBody in web api


@Nkosi 2016-11-29 02:37:42

Referencing Parameter Binding in ASP.NET Web API

Using [FromBody]

To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter:

public IHttpActionResult Test(int id, [FromBody] string jsonString) { ... }

In this example, Web API will use a media-type formatter to read the value of jsonString from the request body. Here is an example client request.

POST http://localhost:8000/Edit/Test?id=111 HTTP/1.1
User-Agent: Fiddler
Host: localhost:8000
Content-Type: application/json
Content-Length: 6


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).

In the above example no model is needed if the data is provided in the correct format in the body.

For URL encoded a request would look like this

POST http://localhost:8000/Edit/Test?id=111 HTTP/1.1
User-Agent: Fiddler
Host: localhost:8000
Content-Type: application/x-www-form-urlencoded
Content-Length: 5


@Igor 2016-11-28 21:26:18

You are on the right track.

On your header set

Content-Type: application/x-www-form-urlencoded

The body of the POST request should be =test and nothing else. For unknown/variable strings you have to URL encode the value so that way you do not accidentally escape with an input character.

See also POST string to ASP.NET Web Api application - returns null

@Ralf Bönning 2016-11-28 21:03:12

By declaring the jsonString parameter with [FromBody] you tell ASP.NET Core to use the input formatter to bind the provided JSON (or XML) to a model. So your test should work, if you provide a simple model class

public class MyModel
    public string Key {get; set;}

public void Test(int id, [FromBody] MyModel model)
    ... model.Key....

and a sent JSON like

    key: "value"

Of course you can skip the model binding and retrieve the provided data directly by accessing HttpContext.Request in the controller. The HttpContext.Request.Body property gives you the content stream or you can access the form data via HttpContext.Request.Forms.

I personally prefer the model binding because of the type safety.

@Fabio 2016-11-28 21:08:25

In question was mentioned: I also want to avoid using a model...

@Ralf Bönning 2016-11-28 21:10:11

@Fabio - then [FromBody] will not work, because it tells the framework you want to bind the data to a model class. In order to avoid the binding, skip this parameter and access the sent data directly as hinted in the last paragraph. Hope that will help.

@Codeman 2018-06-19 23:48:33

This worked for me - you just have to use RAW instead of FORM in the POST options for POSTMan.

@jPhizzle 2019-07-22 18:54:51

worked like a charm, thank you. don't know why we can't just pass a string from body, but... meh... hehe thank you

Related Questions

Sponsored Content

39 Answered Questions

21 Answered Questions

[SOLVED] How do you give a C# Auto-Property a default value?

63 Answered Questions

[SOLVED] What is the difference between String and string in C#?

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

1 Answered Questions

How to test POST method with Postman and x-www-form-urlencoded Body?

37 Answered Questions

[SOLVED] Post parameter is always null

2 Answered Questions

[SOLVED] string value is Empty when using FromBody in web api

1 Answered Questions

[SOLVED] How to make post request with params and body in Postman

  • 2018-04-05 14:41:26
  • Bartek
  • 40232 View
  • 9 Score
  • 1 Answer
  • Tags:   postman

2 Answered Questions

[SOLVED] Convert cURL to Postman REST Call

  • 2018-10-01 14:10:57
  • Sammy
  • 1378 View
  • 1 Score
  • 2 Answer
  • Tags:   rest curl postman

1 Answered Questions

Sponsored Content