本文代码

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:
Host: localhost:
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1. (java 1.5)
"test" response:
HTTP/1.1 OK
Date: Wed, Mar :: GMT
Content-Type: text/plain; charset=utf-
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:
Host: localhost:
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1. (java 1.5)
test response:
HTTP/1.1 OK
Date: Wed, Mar :: GMT
Content-Type: text/plain; charset=utf-
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:
Host: localhost:
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1. (java 1.5)
test response:
HTTP/1.1 OK
Date: Wed, Mar :: GMT
Content-Type: text/plain; charset=utf-
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 = ; 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:
Host: localhost:
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1. (java 1.5)
test response:
HTTP/1.1 OK
Date: Wed, Mar :: GMT
Content-Type: text/plain; charset=utf-
Server: Kestrel
Transfer-Encoding: chunked content: contentFromBody: contentFromBody2: test

.net core web api 获取request body的纯文本的更多相关文章

  1. ASP.NET Core Web APi获取原始请求内容

    前言 我们讲过ASP.NET Core Web APi路由绑定,本节我们来讲讲如何获取客户端请求过来的内容. ASP.NET Core Web APi捕获Request.Body内容 [HttpPos ...

  2. 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)

    对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战.在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你 ...

  3. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  4. ASP.NET Core Web API下事件驱动型架构的实现(一):一个简单的实现

    很长一段时间以来,我都在思考如何在ASP.NET Core的框架下,实现一套完整的事件驱动型架构.这个问题看上去有点大,其实主要目标是为了实现一个基于ASP.NET Core的微服务,它能够非常简单地 ...

  5. ASP.NET Core Web API下事件驱动型架构的实现(二):事件处理器中对象生命周期的管理

    在上文中,我介绍了事件驱动型架构的一种简单的实现,并演示了一个完整的事件派发.订阅和处理的流程.这种实现太简单了,百十行代码就展示了一个基本工作原理.然而,要将这样的解决方案运用到实际生产环境,还有很 ...

  6. 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传

    准备工作: 建立.NET Core Web Api项目 新建一个用于Api请求的UserInfo类 public class UserInfo { public string name { get; ...

  7. .NET Core WEB API中接口参数的模型绑定的理解

    在.NET Core WEB API中参数的模型绑定方式有以下表格中的几种: 微软官方文档说明地址:https://docs.microsoft.com/zh-cn/aspnet/core/web-a ...

  8. [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了

    [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 本文首发自:博客园 文章地址: https://www.cnblogs.com/yilezhu/p/ ...

  9. 循序渐进学.Net Core Web Api开发系列【15】:应用安全

    系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍W ...

随机推荐

  1. best code #54 div 2 A 水

    A problem of sorting Accepts: 443 Submissions: 1696 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  2. scrapy架构设计分析

    scrapy是一个Python爬虫框架.我们自己用requests也能写爬虫(GET某个URL,然后Parse网页的内容),那么,问题来了,scrapy高明在哪些地方呢?下面就来讨论下这个话题,看看业 ...

  3. try catch finally 和return

    结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...

  4. Kubernetes - Launch Single Node Kubernetes Cluster

    Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kuberne ...

  5. intellij idea 破解补丁激活

    一.说明 idea激活可以用JetBrains account,Activation Code注册码或者填License server网址,使用注册码的方式可以参考lanyun提供的注册码,但是有效时 ...

  6. JAVA获取Classpath根路径的方法

    方法一: String path = Test.class.getResource("/").toString(); System.out.println("path = ...

  7. Ant打jar包时,参数名被修改的问题

    https://blog.csdn.net/landehuxi/article/details/42678117 使用Ant打jar包后,发现jar包中的方法名会在前面自动添加了“param”前缀,导 ...

  8. Jmeter 重要测试指标释义

    Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...

  9. .NET Core Data Access

    .NET Core was released a few months ago, and data access libraries for most databases, both relation ...

  10. 在使用ubuntu16.04+python3.5 下使用pip3出现pip3 error - '_NamespacePath' object has no attribute 'sort'

    使用pip3安装tensorflow以及gensim等时,出现如下错误: Traceback (most recent call last): File "/usr/local/bin/pi ...