.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 ...
随机推荐
- apt-get update的hit和ign含义
How do Ign and Hit affect apt-get update? From what I can see in the apt source code, "Ign" ...
- ubuntu10.04 安装gcc4.1.2
After a bunch of searching to get gcc-4.1 & g++-4.1 in Ubuntu 10.10 (maverick), I found easy wor ...
- centos7-每天定时备份 mysql数据库
centos7-每天定时备份 mysql数据库 第一步:编写数据库备份脚本database_mysql_shell.sh #!/bin/bash DATE=`date +%Y%m%d%H%M` #ev ...
- 耐心排序Patience Sorting
这个排序的关键在建桶和入桶规则上 建桶规则:如果没有桶,新建一个桶;如果不符合入桶规则那么新建一个桶 入桶规则:只要比桶里最上边的数字小即可入桶,如果有多个桶可入,那么按照从左到右的顺序入桶即可 举个 ...
- HDU5852 Intersection is not allowed!
There are K pieces on the chessboard. The size of the chessboard is N*N. The pieces are initially pl ...
- HDU 1465 不容易系列之一 (错排公式+容斥)
题目链接 Problem Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好"一件"事情尚且不易,若想永远成功而总从不失败,那更是难上 ...
- C# 文件操作常用方法总结
需引用 System.IO Path为绝对路径 检测指定目录是否存在 Directory.Exists(Path) 创建目录 Directory.CreateDirectory(Path) 删除目录 ...
- Angular2.0 基础: Form
对于Angular2.0 的Form表单中的隐藏和验证,个人觉得还是挺有意思的. 1.通过ngModel 跟踪修改状态与验证. 在表单中使用 ngModel 可以获得更多的控制权,包括一些常用的验证. ...
- Double类型的数据四舍五入保留小数点后两位
4种方法,都是四舍五入,例: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberF ...
- Msfvenom学习总结-MSF反弹webshell
1. –p (- -payload-options) 添加载荷payload. 载荷这个东西比较多,这个软件就是根据对应的载荷payload生成对应平台下的后门,所以只有选对payload,再填 ...