无法接收3ds方POST请求。 415不支持的媒体类型或空模型
在后端:Asp.net core 2.2和Aspnetbilerplate(如果重要)
来自第三方服务器的请求,所以我不能影响它。我只能指定将请求发送到的端点
看起来像: curl http://MyServer/api/MyController/MyAction -d'{“ a”:“ a”,“ b”:1}'
我的代码 Dto:
public class testDto
{
public string A { get; set; }
public int B { get; set; }
}
控制器:
[Route("api/[controller]/[action]")]
public class MyController : MyControllerBase
{
...
[HttpPost]
public async Task<testDto> MyAction(testDto dto)
{
//some code
_logger.Info("test");
}
...
}
我从控制台发出的测试结果请求:
C:\WINDOWS\system32>curl -d '{"a":"a", "b":1}' http://myServerUrl/api/MyController/MyAction
curl: (3) [globbing] unmatched close brace/bracket in column 4
{"result":{"a":null,"b":0},"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}
模型为空,未发生绑定。
我被添加了[FromBody]这样的动作:
[HttpPost]
public async Task<testDto> MyAction([FromBody]testDto dto)
{
//some code
_logger.Info("test");
}
结果:HTTP状态码415
此外,尝试添加[FromForm]和[FromQyesry]。结果:空模型
出什么问题了?我该如何运作? 预先谢谢你。
答案 0 :(得分:1)
第一件事是请求$has_no_end_date
不正确,您可以看到协议的使用:
curl 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
您应该与3ds party联系以确认请求。
无论如何,如果默认模型绑定不符合您的要求,则可以创建Custom Model Binding:
添加中间件以发出请求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
:
EnableRewind
创建实现app.Use(async (ctx, next) =>
{
ctx.Request.EnableRewind();
await next();
});
的自定义资料夹:
IModelBinder
使用活页夹:
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;
}
}