讀取請求體流的demo public static async Task<string> GetBodyForm(this HttpContext http) { var content = string.Empty; var request = http.Request; try { reques ...
讀取請求體流的demo
public static async Task<string> GetBodyForm(this HttpContext http)
{
var content = string.Empty;
var request = http.Request;
try
{
request.Body.Position = 0;
using var reader = new StreamReader(request.Body, Encoding.UTF8, leaveOpen: true);
var strRequestBody = await reader.ReadToEndAsync();
Console.WriteLine("ok");
Console.WriteLine(strRequestBody == null ? "null" : strRequestBody);
request.Body.Position = 0;
}
catch (Exception ex)
{
Console.WriteLine("err");
Console.WriteLine(ex);
}
return content;
}
在ActionFilter中讀取Request.Body
public class ActionFilterTestA : ActionFilterAttribute
{
public override async void OnActionExecuting(ActionExecutingContext context)
{
Console.WriteLine("From Request Body---->");
Console.WriteLine(await context.HttpContext.GetBodyForm());
Console.WriteLine("From Request Body---->");
}
}
報錯,一般是在Request.Body處報NotSupportedException
解決方案
在自定義中間件中調用EnableBuffering()
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next();
});
疑問
(移除以上正確方案代碼) 為什麼在ActionFilterTestA
中調用context.HttpContext.Request.EnableBuffering();
沒有效果?(沒有報錯,但是內容為空字元串)
猜測
請求體流在ActionFilter之前,在自定義中間件之後被消耗。 中間件執行順序
測試
// 取消模型綁定
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressInferBindingSourcesForParameters = true;
});
Request.Body的內容列印成功。
推論
綁定模型時會消耗掉請求體流。
其他資料
https://markb.uk/asp-net-core-read-raw-request-body-as-string.html
結語
推薦還是通過中間件調用EnableBuffering
解決問題
註意事項:
- 避免關閉StreamReader導致關閉了Stream,可以通過
leaveOpen: true
解決 - 重置Position = 0