The. NET core Web API gets the plain text of the request body

Source: Internet
Author: User

This article code

Https://github.com/wuhaibo/readPlainTextDotNetCoreWepApi

There are always times when we want to get the pure text of request body. Very simple. As shown below

         Public string Getjsonstring ([frombody]string  content)        {            return"" + content;        }

The test results are as follows

request:post http://localhost:5000/api/values/getjsonstring http/1.1accept-encoding:gzip,deflatecontent-type:application/jsoncontent-length:6Host:localhost: theConnection:keep-Aliveuser-agent:apache-httpclient/4.1.1(Java1.5)"Test"response:http/1.1  $okdate:wed, -Mar2018  -: $:Panax Notoginsenggmtcontent-type:text/plain; charset=utf-8Server:kestreltransfer-Encoding:chunkedcontent:test

You can see that the content is assigned the test. But there is a question that the content of the request body must be a valid JSON and the media type of request is JSON

To give an example,

request:post http://localhost:5000/api/values/getjsonstring http/1.1accept-encoding:gzip,deflatecontent-type:application/xmlcontent-length:4Host:localhost: theConnection:keep-Aliveuser-agent:apache-httpclient/4.1.1(Java1.5) Testresponse:http/1.1  $okdate:wed, -Mar2018  -: to: -gmtcontent-type:text/plain; charset=utf-8Server:kestreltransfer-encoding:chunkedcontent:

You can see that the contents of the request body test is not a valid XML, so the content we return is empty.

A better way to do this is to get the plain text of the request body, either media type, as shown below.

         Public string GetJsonString3 (string  content)        {            varnew  StreamReader ( Request.body);             var contentfrombody = reader. ReadToEnd ();             return "   "+ content                    "" + contentfrombody;        }

Test results

request:post http://Localhost:5000/api/values/getjsonstring3 http/1.1accept-encoding:gzip,deflatecontent-type:application/xmlcontent-length:4Host:localhost: theConnection:keep-Aliveuser-agent:apache-httpclient/4.1.1(Java1.5) Testresponse:http/1.1  $okdate:wed, -Mar2018  -: +: -gmtcontent-type:text/plain; charset=utf-8Server:kestreltransfer-Encoding:chunkedcontent:contentFromBody:test

We can see the contents of the request body in Contentfrombody. Note that the parameter does not have [frombody] If this attribute is added, then if the request body content matches the request's media type then request.body position will be placed at the end of the position. As an example,

 Public stringGetJsonString2 ([Frombody]stringcontent) {            varReader =NewStreamReader (request.body); varContentfrombody =Reader.                        ReadToEnd (); Request.Body.Position=0; varReader2 =NewStreamReader (request.body); varContentFromBody2 =Reader2.            ReadToEnd (); return "content:"+content+"Contentfrombody:"+Contentfrombody+"ContentFromBody2:"+ContentFromBody2; }

Test results

request:post http://localhost:5000/api/values/getjsonstring2 http/1.1accept-encoding:gzip,deflatecontent-type:application/jsoncontent-length:4Host:localhost: theConnection:keep-Aliveuser-agent:apache-httpclient/4.1.1(Java1.5) Testresponse:http/1.1  $okdate:wed, -Mar2018  -: -:Genevagmtcontent-type:text/plain; charset=utf-8Server:kestreltransfer-Encoding:chunkedcontent:contentFromBody:contentFromBody2:test

The. NET core Web API gets the plain text of the request body

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.