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