Null value when Pass values [FromBody] to post method by Postman plugin

Saeed Ahmadian Masal :

I use api controller in ASP.net web API and i need to pass value to post method by [FromBody] type..

 [HttpPost]
 public HttpResponseMessage Post( [FromBody]string name)
 {
     ....
 }

i use Postman plugin but when send to post method value of name always is null.. follow this image: enter image description here

and in Post methods : enter image description here

why this happend?!

Igor :

You can't bind a single primitive string using json and FromBody, json will transmit an object and the controller will expect an complex object (model) in turn. If you want to only send a single string then use url encoding.

On your header set

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

The body of the POST request message body should be =saeed (based on your test value) 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.


Alternate 1

Create a model and use that instead.

Message body value: {"name":"saeee"}

c#

public class CustomModel {
    public string Name {get;set;}
}

Controller Method

public HttpResponseMessage Post([FromBody]CustomModel model)

Alternate 2

Pass primitive strings to your post using the URI instead of the message body.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Null value comes from Post method(FromBody) when api is invoked from powershell

WebApi - action paramer is null when using [FromBody] attribute and POST method

With "frombody" attribute, parameter of Web Api method recieves null value while testing it with Postman

ASP.NET Core FromBody is null on AJAX POST but populates in Postman

When post request is done in postman it passes all the values as NULL

Post FromBody Always Null

WebApi2 Controller Post method always receiving null FromBody

Why the webapi's post method is receiving the parameter of type interface as null when called from a postman?

null values passed when passing ITestContext attribute value to onTestStart method

Cannot post value to database Using API post method and postman

Null values in Post call via Spring, but in Postman is working

Unable to pass form values using POST method

Null values for both @RequestBody Objects sent with PUT method on Postman to RestController?

What causes a method to pass null values?

Postman returns null value after a successful post request

How to pass null value in fetch POST request

Pass Value To Another Page Using Post Method

Pass value to controller from post-method

Jersey POST Method is receiving null values as parameters

Web API parameter values are null when check with Postman

Pass null value when method declaration creates an empty map - input: Map<String, String> = emptyMap() in the interface - Kotlin

FromBody is null

Null Value when passing JSON data from POSTMAN to Controller

Extra null value when i try to return a message in postman

Wordpress plugin using php method="post" not passing values

Postman POST to WCF object is null

Is there a default Pandas method for removing null or missing values when they are represented by a custom value like "?" or "Unknown"

How to pass the correct value when calling a method?

How to pass complex values to Body of a HTTPRequest in a POST method