.net core web api 获取request body的纯文本

 

本文代码

https://github.com/wuhaibo/readPlainTextDotNetCoreWepApi

 

 

总有些时候我们希望获得Request body 的纯文本 那么怎么做呢?很简单。如下所示

        public string GetJsonString([FromBody]string content)
        {
            return "content: " + content ;
        }

 

测试结果如下

request:
POST http://localhost:5000/api/values/GetJsonString HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 6
Host: localhost:5000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
"test"

response:
HTTP/1.1 200 OK
Date: Wed, 07 Mar 2018 14:36:37 GMT
Content-Type: text/plain; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked

content: test 

 

可以看到content被赋值test。 但有个问题request body的内容必须是合法的json而且request 的media type也得是json

举个例子,

request:
POST http://localhost:5000/api/values/GetJsonString HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/xml
Content-Length: 4
Host: localhost:5000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
test

response:
HTTP/1.1 200 OK
Date: Wed, 07 Mar 2018 14:31:07 GMT
Content-Type: text/plain; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked

content:  

 

可以看到由于request body的内容 test 并不是个合法的xml,所以我们返回的content是空。

有个更好的方法 如下所示,这种方法不论是media type都可以获得request body 的纯文本

        public string GetJsonString3(string content)
        {
            var reader = new StreamReader(Request.Body);
            var contentFromBody = reader.ReadToEnd();
            return "content: " + content 
                   + " contentFromBody: " + contentFromBody;
        }

 

测试结果

request:
POST http://localhost:5000/api/values/GetJsonString3 HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/xml
Content-Length: 4
Host: localhost:5000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
test

response:
HTTP/1.1 200 OK
Date: Wed, 07 Mar 2018 14:43:59 GMT
Content-Type: text/plain; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked

content:  contentFromBody: test 

 

可以看到contentFromBody中我们得到了request body的内容。 注意参数没有[FromBody]这个属性 如果加了这个属性,那么如果request body内容匹配request的media type那么Request.body的position会被置于结尾的位置。 举个例子

        public string GetJsonString2([FromBody]string content)
        {

            var reader = new StreamReader(Request.Body);
            var contentFromBody = reader.ReadToEnd();
            
            Request.Body.Position = 0;
            
            var reader2 = new StreamReader(Request.Body);
            var contentFromBody2 = reader2.ReadToEnd();

            return "content: " + content 
                   + " contentFromBody: " + contentFromBody
                   + " contentFromBody2: " + contentFromBody2;
        }

 

测试结果

request:
POST http://localhost:5000/api/values/GetJsonString2 HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 4
Host: localhost:5000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
test

response:
HTTP/1.1 200 OK
Date: Wed, 07 Mar 2018 14:53:03 GMT
Content-Type: text/plain; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked

content:  contentFromBody:  contentFromBody2: test

 

转载于:https://www.cnblogs.com/williamwood/p/8525973.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值