POST 对 ASP.NET 核心 6 的请求的错误请求 (400),最小 API,从 Postman 发送
Bad Request (400) for POST request to ASP.NET Core 6, minimal API, sent from Postman
为什么我在使用 Postman 测试 ASP.NET 核心最小 API 应用程序时收到 400 个错误请求?
默认 VS 2022 ASP.NET 核心最小 API 项目,具有以下方法处理程序:
app.MapPost("/test", (AnalizeSentenceRequest r) =>
{
return Results.Ok($"Echo: {r.Sentence}");
});
DTO:
public class AnalizeSentenceRequest
{
public string? Sentence { get; set; }
}
Windows 10 或 Ubuntu 卷发效果很好:
> curl -X POST http://localhost:5050/test -H "Content-Type: application/json" -d "{\"sentence\":\"testing\"}"
回复:
"Echo: testing"
但是 Postman 得到 400:
邮递员headers:
编辑:邮递员回复:
它抱怨无法绑定到 DTO AnalizeSentenceRequest r
我尝试将 [FromBody] 属性添加到处理程序:
([FromBody] AnalizeSentenceRequest r) => ...
但这没有什么区别。
尽管它与 curl 一起工作...所以看起来 Postman 搞砸了 body,但它可能是什么?
完整回复:
Microsoft.AspNetCore.Http.BadHttpRequestException: Required parameter "AnalizeSentenceRequest r" was not provided from body.
at Microsoft.AspNetCore.Http.RequestDelegateFactory.Log.RequiredParameterNotProvided(HttpContext httpContext, String parameterTypeName, String parameterName, String source, Boolean shouldThrow)
at lambda_method1(Closure , Object , HttpContext , Object )
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass46_3.<<HandleRequestBodyAndCompileRequestDelegate>b__2>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: */*
Host: localhost:5050
Content-Type: application/json
在 Postman 中勾选 Content-Length 复选框。原因是 Content-Length header 对于具有实体主体的消息是强制性的,除非使用分块编码传输消息。需要 Content-Length 来检测服务器崩溃时过早的消息截断并正确分割共享持久连接的消息。
为什么我在使用 Postman 测试 ASP.NET 核心最小 API 应用程序时收到 400 个错误请求?
默认 VS 2022 ASP.NET 核心最小 API 项目,具有以下方法处理程序:
app.MapPost("/test", (AnalizeSentenceRequest r) =>
{
return Results.Ok($"Echo: {r.Sentence}");
});
DTO:
public class AnalizeSentenceRequest
{
public string? Sentence { get; set; }
}
Windows 10 或 Ubuntu 卷发效果很好:
> curl -X POST http://localhost:5050/test -H "Content-Type: application/json" -d "{\"sentence\":\"testing\"}"
回复:
"Echo: testing"
但是 Postman 得到 400:
邮递员headers:
编辑:邮递员回复:
它抱怨无法绑定到 DTO AnalizeSentenceRequest r
我尝试将 [FromBody] 属性添加到处理程序:
([FromBody] AnalizeSentenceRequest r) => ...
但这没有什么区别。
尽管它与 curl 一起工作...所以看起来 Postman 搞砸了 body,但它可能是什么?
完整回复:
Microsoft.AspNetCore.Http.BadHttpRequestException: Required parameter "AnalizeSentenceRequest r" was not provided from body.
at Microsoft.AspNetCore.Http.RequestDelegateFactory.Log.RequiredParameterNotProvided(HttpContext httpContext, String parameterTypeName, String parameterName, String source, Boolean shouldThrow)
at lambda_method1(Closure , Object , HttpContext , Object )
at Microsoft.AspNetCore.Http.RequestDelegateFactory.<>c__DisplayClass46_3.<<HandleRequestBodyAndCompileRequestDelegate>b__2>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
HEADERS
=======
Accept: */*
Host: localhost:5050
Content-Type: application/json
在 Postman 中勾选 Content-Length 复选框。原因是 Content-Length header 对于具有实体主体的消息是强制性的,除非使用分块编码传输消息。需要 Content-Length 来检测服务器崩溃时过早的消息截断并正确分割共享持久连接的消息。