Curl 无法接收POST请求Asp.net core 2.2
无法接收3ds party POST请求。415不支持的媒体类型或空模型 在后端:Asp.net core 2.2和AspNetBilerPlate(如果重要的话) 来自第三方服务器的请求,因此我无法影响它。我只能指定将请求发送到的端点 看起来是这样的: 旋度-d'{a:a,b:1}' 我的代码 Dto: 控制器:Curl 无法接收POST请求Asp.net core 2.2,curl,asp.net-core,http-post,asp.net-core-webapi,aspnetboilerplate,Curl,Asp.net Core,Http Post,Asp.net Core Webapi,Aspnetboilerplate,无法接收3ds party POST请求。415不支持的媒体类型或空模型 在后端:Asp.net core 2.2和AspNetBilerPlate(如果重要的话) 来自第三方服务器的请求,因此我无法影响它。我只能指定将请求发送到的端点 看起来是这样的: 旋度-d'{a:a,b:1}' 我的代码 Dto: 控制器: [Route("api/[controller]/[action]")] public class MyController : MyControllerBase { ...
[Route("api/[controller]/[action]")]
public class MyController : MyControllerBase
{
...
[HttpPost]
public async Task<testDto> MyAction(testDto dto)
{
//some code
_logger.Info("test");
}
...
}
模型为空,未发生绑定
我被添加到这样的动作中:
[HttpPost]
public async Task<testDto> MyAction([FromBody]testDto dto)
{
//some code
_logger.Info("test");
}
结果:HTTP状态代码415
另外,尝试添加[FromForm]和[FromQyesry]。结果:空模型
有什么问题?我如何让它工作?
提前谢谢。首先是请求http://MyServer/api/MyController/MyAction -d'{a:a,b:1}'不正确,您可以看到协议使用:
curl http://MyServer/api/MyController/MyAction -d '{"a":"a", "b":1}' --trace-ascii debugdump.txt
如果检查转储文件,您会发现数据未完全发送:
0000: POST /api/values/MyAction HTTP/1.1
0024: Host: localhost:44348
003b: User-Agent: curl/7.55.1
0054: Accept: */*
0061: Content-Length: 6
0074: Content-Type: application/x-www-form-urlencoded
00a5:
=> Send data, 6 bytes (0x6)
0000: '{a:a,
== Info: upload completely sent off: 6 out of 6 bytes
== Info: schannel: client wants to read 102400 bytes
== Info: schannel: encdata_buffer resized 103424
== Info: schannel: encrypted data buffer: offset 0 length 103424
== Info: schannel: encrypted data got 322
== Info: schannel: encrypted data buffer: offset 322 length 103424
您应该与3ds方联系以确认请求
无论如何,如果默认模型绑定不符合您的要求,您可以创建:
添加中间件以启用请求回放:
创建实现IModelBinder的自定义活页夹:
使用活页夹:
[ModelBinder(BinderType = typeof(testDtoEntityBinder))]
public class testDto
{
public string A { get; set; }
public int B { get; set; }
}
curl-d发布内容类型application/x-www-form-urlencoded,而不是application/json。第三方是否发送了此类请求,或者您的curl设置有误?第三方发送给我的curl格式与此完全相同。我只能以某种方式适应=在这种情况下,内容类型和数据不匹配;请查看答案,在my case中不幸无效=字符串参数仍然为null[HttpPost][Consumesapplication/x-www-form-urlencoded]公共异步任务MyAction[FromForm]字符串数据{var dto=JsonConvert.DeserializeObjectdata;return dto;}也已尝试[FromBody]-错误415
0000: POST /api/values/MyAction HTTP/1.1
0024: Host: localhost:44348
003b: User-Agent: curl/7.55.1
0054: Accept: */*
0061: Content-Length: 6
0074: Content-Type: application/x-www-form-urlencoded
00a5:
=> Send data, 6 bytes (0x6)
0000: '{a:a,
== Info: upload completely sent off: 6 out of 6 bytes
== Info: schannel: client wants to read 102400 bytes
== Info: schannel: encdata_buffer resized 103424
== Info: schannel: encrypted data buffer: offset 0 length 103424
== Info: schannel: encrypted data got 322
== Info: schannel: encrypted data buffer: offset 322 length 103424
app.Use(async (ctx, next) =>
{
ctx.Request.EnableRewind();
await next();
});
public class testDtoEntityBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var body = bindingContext.HttpContext.Request.Body;
body.Position = 0;
string raw = new System.IO.StreamReader(body).ReadToEnd();
//now read content from request content and fill your model
var result = new testDto
{
A = "",
B = 1,
};
bindingContext.Result = ModelBindingResult.Success(result);
return Task.CompletedTask;
}
}
[ModelBinder(BinderType = typeof(testDtoEntityBinder))]
public class testDto
{
public string A { get; set; }
public int B { get; set; }
}