Calling POST method from API controller
Solution 1
Create a class that corresponds to your JSON:
public class Test
{
public string value{get; set;}
public int ID {get; set;}
}
And then change your Api-action:
// POST api/myfiles
public void Post([FromBody]Test value)
{
}
If you don't want to do that, just change the POST-body:
"somevalue"
EDIT: Added ID to the POST-payload. Now your JSON should look like this:
{"value": "someval",
"ID": 1}
Solution 2
According to the MVC document http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-1, Which exactly shows the same case as yours
Go to the Sending Simple Types part.
When you sending simple value, you need to
Use FromBody Attribute
-
the client needs to send the value with the following format:
=value
Specifically, the name portion of the name/value pair must be empty for a simple type.
Anyway, for further enhancement, you'd better use a complex type (an object) to accept the parameter.
Solution 3
You need to change from:
Request body:
{"value": "asjkfsf"}
To:
Request body:
"asjkfsf"
It will work for simple type with json format
petko_stankoski
Updated on June 08, 2022Comments
-
petko_stankoski almost 2 years
I have an API controller named MyFIlesController.
In it, I have this method:
// POST api/myfiles public void Post([FromBody]string value) { }
And here's how I call it with Fiddler:
POST URL: `http://localhost:58075/api/myfiles` ------------------------- Request Header: User-Agent: Fiddler Host: localhost:58075 Content-Type: application/json Content-length: 18 ------------- Request body: {"value": "asjkfsf"}
The method gets called, but value is null. What am I doing wrong?
-
J0e3gan almost 11 yearsAre you showing a Fiddler-logged request that your code made or a request that you built in Fiddler?
-
graumanoz almost 11 yearsAdd [HttpPost] attribute to this method
-
petko_stankoski almost 11 years@J0e3gan A request that I built in Fiddler.
-
Tommi almost 11 years'{"value": "asjkfsf"}'.length -> 20; not probably the reason of issue, but nevertheless.
-
J0e3gan almost 11 yearsSo it sounds like the question is why
Post
's caller is passingnull
forvalue
, correct? -
petko_stankoski almost 11 years@Tommi The content length should be the length of {"value": "asjkfsf"} string?
-
Tommi almost 11 yearsAFAIK Content-Length should be your whole request body length.
-
-
petko_stankoski almost 11 yearsAnd what if the method has two input parameters? ([FromBody]string value, int id)
-
petko_stankoski almost 11 yearsAnd what if the method has two input parameters? ([FromBody]string value, int id)
-
cuongle almost 11 years@petko_stankoski: if you use id for just query, put it on querystring
?id=...
. For more, you really need a model -
Kenneth almost 11 yearsDo you want the id inside the JSON or as a Url-part?
-
petko_stankoski almost 11 yearsHow can I use ?id= for POST method?
-
petko_stankoski almost 11 yearsI want the id to be in the Request Body.
-
cuongle almost 11 years@petko_stankoski: just put on URL
URL: http://localhost:58075/api/myfiles?id=...
-
petko_stankoski almost 11 yearsHmm so there isn't a way without craeting the new class?
-
Kenneth almost 11 yearsNo, there isn't, unless you're willing to pass it in the URL. Then you can just add the parameter to the action method and it will just work. But not through JSON no