.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:
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的纯文本的更多相关文章
- ASP.NET Core Web APi获取原始请求内容
前言 我们讲过ASP.NET Core Web APi路由绑定,本节我们来讲讲如何获取客户端请求过来的内容. ASP.NET Core Web APi捕获Request.Body内容 [HttpPos ...
- 使用 Swagger 自动生成 ASP.NET Core Web API 的文档、在线帮助测试文档(ASP.NET Core Web API 自动生成文档)
对于开发人员来说,构建一个消费应用程序时去了解各种各样的 API 是一个巨大的挑战.在你的 Web API 项目中使用 Swagger 的 .NET Core 封装 Swashbuckle 可以帮助你 ...
- 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持
HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...
- ASP.NET Core Web API下事件驱动型架构的实现(一):一个简单的实现
很长一段时间以来,我都在思考如何在ASP.NET Core的框架下,实现一套完整的事件驱动型架构.这个问题看上去有点大,其实主要目标是为了实现一个基于ASP.NET Core的微服务,它能够非常简单地 ...
- ASP.NET Core Web API下事件驱动型架构的实现(二):事件处理器中对象生命周期的管理
在上文中,我介绍了事件驱动型架构的一种简单的实现,并演示了一个完整的事件派发.订阅和处理的流程.这种实现太简单了,百十行代码就展示了一个基本工作原理.然而,要将这样的解决方案运用到实际生产环境,还有很 ...
- 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传
准备工作: 建立.NET Core Web Api项目 新建一个用于Api请求的UserInfo类 public class UserInfo { public string name { get; ...
- .NET Core WEB API中接口参数的模型绑定的理解
在.NET Core WEB API中参数的模型绑定方式有以下表格中的几种: 微软官方文档说明地址:https://docs.microsoft.com/zh-cn/aspnet/core/web-a ...
- [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了
[译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 本文首发自:博客园 文章地址: https://www.cnblogs.com/yilezhu/p/ ...
- 循序渐进学.Net Core Web Api开发系列【15】:应用安全
系列目录 循序渐进学.Net Core Web Api开发系列目录 本系列涉及到的源码下载地址:https://github.com/seabluescn/Blog_WebApi 一.概述 本篇介绍W ...
随机推荐
- UVA11426
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page ...
- 文件上传文件的权限--lnmp 环境配置,尤其整个项目复制过来动~~~
site_upload 需要是自己才建立动才会是root root 所有者:---不是root : root 上传不了文件,不是 777 就都可以上传的,也要看看是谁建立的文件夹: 打包 ...
- MFC中用正则表达式进行有效性验证
转载自:http://blog.csdn.net/jinhill/article/details/5928993 正则表达式最实用的一个地方是验证用户输入.它可以轻松验证邮编.电话号码.信用卡号码-- ...
- 使用Docker搭建Django,Nginx,R,Python部署环境
转载自https://blog.csdn.net/The_One_is_all/article/details/76063968 基本环境: Ubuntu 16.10 docker 17.06.0-c ...
- redhat 7 安装oracle12.1
https://oracle-base.com/articles/12c/oracle-db-12cr1-installation-on-oracle-linux-7 一定要配置yum本地源 ...
- 【洛谷 P2761】 软件补丁问题(状态压缩,最短路)
题目链接 第四题. 初看题目很懵,网络流这么厉害的吗,毫无头绪去看题解.. 所以这和网络流有什么关系呢? 把规则用二进制保存下来,然后跑最短路救星了. 在线跑,离线连边太慢了. (以后干脆不管什么题直 ...
- NightMare2(SCU4527+dijkstra+二分)
题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4527 题目: 题意:最短路的每条边除了边权之外还会有一个限制(财富,身上带的财富大于这个值则不能通 ...
- Mayor's posters(线段树+离散化+区间染色)
题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...
- 跨域iframe高度计算
一.同域获取iframe内容 这里有两个细节: 1. 取iframe内的文档对象,标准浏览器使用contentDocument属性,IE低版本(IE6,7,8)使用document属性. 2. cal ...
- Linux 入门记录:十五、Linux 网络基本配置
一.以太网(Ethernet) 以太网(Ethernet)是一种计算机局域网技术.IEEE 组织的 IEEE 802.3 标准制定了以太网的技术标准,它规定了包括物理层的连线.电子信号和介质访问层协议 ...