2

I am using asp.net core web api. below is my simple post function which is having a single string parameter. The problem is when I use [FromBody] the string stays null. I am using PostMan to test my service. I want raw data to pass from client to my controller. In Postman I am selecting body type RAW and I set the header Content-Type text/plain. The Raw Body contains Just "Hello World" string.

[HttpPost]
        [Route("hosted-services/tokenize-card")]
        public IActionResult Test([FromRoute]decimal businessKey,[FromBody] string body)
        {
            var data = businessKey;
            return new JsonResult("Hello World");
        }
maxspan
  • 13,326
  • 15
  • 75
  • 104
  • 1
    The content of your POST request must be formated with a registered content-type. If it's application/json the content must be ```"mystring"``` with the double quote – Kalten Jan 10 '17 at 00:07
  • I have updated my question – maxspan Jan 10 '17 at 00:12
  • 1
    [Source](https://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api). When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. Only XML and JSON content-types are supported by default. – Kalten Jan 10 '17 at 00:19
  • I think Kalten has identified the issue, try using "application/json" or "text/json" as the value for the "Content-Type" header in Postman – Peter Jan 10 '17 at 00:24
  • so should I make my raw data like {"body": "Hello World"} – maxspan Jan 10 '17 at 00:37
  • 2
    Your parameter is a string, so your raw content must be a json string. "Hello World" **WITH** the **"** – Kalten Jan 10 '17 at 00:41
  • Nice to know that FromBody only support XML and Json. Thanks – maxspan Jan 10 '17 at 00:51
  • 1
    Possible duplicate of [FromBody string parameter is giving null](https://stackoverflow.com/questions/40853188/frombody-string-parameter-is-giving-null) – Michael Freidgeim Nov 18 '17 at 01:19

3 Answers3

7

Like the doc says :

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter.

Only XML and JSON content-types are supported by default. So you need to use application/xml, application/json or register a custom IInputFormatter.

Next, you need to send a content that match the selected content-type. For json, if the parameter is int send a number. If it's a class, send a json object. If it's a string, send a json string. Etc.

int => 14
string => "azerty"
class => { "propName" : "value" }
Array => []
... => ...

In your case you should send application/json content-type and as content :

"Hello string"

And not just

Hello string

Aspnet core json input formatter implementation

Aspnet core xml input formatter implementation

Example: Creating a CSV Media Formatter

Kalten
  • 4,092
  • 23
  • 32
  • For .net core 3.1 post(url, JSON.stringify(yourVariable)) worked like charm at the controller MyMethod([FromBody] string yourVariable) – HydPhani Aug 05 '20 at 20:32
0

I did make it work by passing as row data in PostMan. Do not forget to add "=" sign as prefix in to the values.

 public string Post([FromBody]string value)
 {
     return value;
 }

enter image description here

Emaro
  • 1,397
  • 1
  • 12
  • 21
mzonerz
  • 1,220
  • 15
  • 22
  • 2
    Please = did not work.. For .net core 3.1 post(url, JSON.stringify(yourVariable)) worked like charm at the controller MyMethod([FromBody] string yourVariable) – HydPhani Aug 05 '20 at 20:33
0

Frombody means that get body directly.it is not necessary to use variable and value pairs.

For Example:

Your controller is like this.

[HttpPost]
public IActionResult GetStringFromBody([FromBody] string token)
    {
    ...
    }

False:

False

True:

True

CagriK
  • 121
  • 1
  • 7