Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

c# - Post with int/string (simple type) in body to asp.net core web api 2.1 not working

I'm simply having no luck sending an url encoded form value from postman to a vanilla asp.net core 2.1 web api created with file->new project. I do nothing to it whatsoever but still the new model validation feature seems to kick in and returns a 400 Bad Request to postman. Can anyone tell me what I'm doing wrong?

Controller Action:

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

Raw request (as seen in fiddler):

POST http://localhost:60843/api/values HTTP/1.1
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: a791eee7-63ff-4106-926f-2s67a8dcf37f
User-Agent: PostmanRuntime/7.3.0
Accept: */*
Host: localhost:60843
accept-encoding: gzip, deflate
content-length: 7
Connection: keep-alive

value=test

Raw response:

HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
X-SourceFiles: =?UTF-8?BQzpcUmVwb3NcVGVzdGJlZFxNb2RlbEJpbmRpbmdcTW9kZWxCaW5kaW5nXGFwaVx2YWx1ZXM=?=
X-Powered-By: ASP.NET
Date: Thu, 25 Oct 2018 15:23:49 GMT

21
{"":["The input was not valid."]}
0

Note again that this is the default template for asp.net web api in Visual Studio 2017.

An interesting fact is that if I add Swashbuckle and go to the swagger ui endpoint and use the built in "try it" functionality it produces an error as well, out of the box.

I've gotten this to work with a complex type and a json body, but I can not get the simple type to work and I've tried with all sorts of different content-types.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For those who stumble upon this question, starting with version 2.1 automatic model binding is applied by default and a 400 Bad Request is returned if model binding fails (change from earlier versions which required you to check ModelState.IsValid to see if model binding was successful).

When posting simple types to an asp.net core controller action you now have to specify where it is coming from. Complex types can be inferred but simple types like int and string cannot (if they're in the message body that is. They are inferred if they're in either the query string or route(url)). The two options I see to pass values in the body itself are:

  1. By way of url encoded form value (add [FromForm] to your parameter in the action)

Request from postman:

POST http://your-url/api/values HTTP/1.1
Content-Type: application/x-www-form-urlencoded

value=test

Action method signature:

[HttpPost]
public void Post([FromForm]string value)
{
}
  1. By way of a json body (add [FromBody] to your parameter in the action)

Request from Postman:

POST http://your-url/api/values HTTP/1.1
Content-Type: application/json

"test"

Action method signature:

[HttpPost]
public void Post([FromBody]string value)
{
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...