[英]Trying to reach API using Content-Type application/x-www-form-urlencoded
I am building API restful server using dotnet core framework. 我正在使用dotnet核心框架构建API restful服务器。 I added my controller and trying to reach an endpoint using postman. 我添加了我的控制器并尝试使用邮递员到达终端。
I have 2 problems 我有2个问题
// POST api/user [HttpPost] [Authorize()] public async Task<IActionResult> Post([FromBody]UserModel user) { }
Unless I send the request from postman as application/json
by typing raw json, I can't reach this endpoint if I use application/x-www-form-urlencoded
instead I always get 415 unsupported media type 除非我通过键入raw json从postman发送请求作为application/json
,否则如果我使用application/x-www-form-urlencoded
而无法访问此端点,那么我总是得到415不支持的媒体类型
// POST api/user/avatar [HttpPost] [Authorize()] [Route("avatar")] public async Task<IActionResult> Post([FromBody]UserModel user, [FromBody]IFormFile file) { }
I don't know how to reach such an endpoint using postman 我不知道如何使用邮递员达到这样的终点
In problem 1 you just need to use FromForm attribute instead of FromBody. 在问题1中,您只需要使用FromForm属性而不是FromBody。 As for second, i think will be easier write simple unit test or use Swashbuckle , it has great interface for such requests 至于第二种,我认为更容易编写简单的单元测试或使用Swashbuckle ,它有很好的接口来满足这类请求
You can use Consumes
attribute like that: 您可以像这样使用Consumes
属性:
// POST api/user
[HttpPost, Consumes("application/x-www-form-urlencoded")]
[Authorize()]
public async Task<IActionResult> Post([FromBody]UserModel user)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.