POST请求中的内容为空

时间:2015-09-04 13:45:19

标签: http-post asp.net-web-api2

我使用以下代码作为对应于POST请求的控制器操作。

[HttpPost]
public HttpResponseMessage Post([FromBody]string value)
        {
            var response = new HttpResponseMessage(HttpStatusCode.Accepted)
            {
                Content = new StringContent(value, Encoding.UTF8, "text/xml")
            };
            return response;
        }

我正在使用Firefox的RESTClient插件生成请求。但是,每次值结果为null。请告诉我我做错了什么。

Fiddler收到的回复如下:

POST http://localhost/api/default/values HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: null
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 5
Cookie: ai_user=5EE27B20-4A54-4651-820A-9D9654C2D891|2015-07-28T13:00:05.013Z
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

qwert

更新1: 根据评论,我尝试了“value =”格式,但它也没有帮助。

POST http://localhost/api/default/values HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: null
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 11
Cookie: ai_user=5EE27B20-4A54-4651-820A-9D9654C2D891|2015-07-28T13:00:05.013Z
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

value=qwert

上面的代码片段是新请求的小提取器。还试过JSON请求;但这也不起作用。

POST http://localhost/api/default/values HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: null
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=UTF-8
Content-Length: 17
Cookie: ai_user=5EE27B20-4A54-4651-820A-9D9654C2D891|2015-07-28T13:00:05.013Z
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

{"value"="qwert"}

1 个答案:

答案 0 :(得分:0)

在发出POST请求时,使用application / x-www-form-urlencoded作为内容类型。