问题描述
我正在使用dotnet核心框架构建API Restful服务器。我添加了控制器并尝试使用邮递员到达端点。
I am building API restful server using dotnet core framework. I added my controller and trying to reach an endpoint using postman.
我有2个问题
-
问题1
Problem 1
// POST api/user
[HttpPost]
[Authorize()]
public async Task<IActionResult> Post([FromBody]UserModel user)
{
}
除非我通过输入raw json以 application / json
的身份从邮递员发送请求,否则如果使用<$则无法到达此终结点c $ c> application / x-www-form-urlencoded 相反,我总是得到415种不受支持的媒体类型
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
-
问题2
Problem 2
// 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
推荐答案
在问题1中,您只需要使用FromForm属性而不是FromBody。
至于第二个,我认为编写简单的单元测试或使用,它对于此类请求具有出色的界面
In problem 1 you just need to use FromForm attribute instead of FromBody.As for second, i think will be easier write simple unit test or use Swashbuckle, it has great interface for such requests
这篇关于尝试使用Content-Type应用程序/ x-www-form-urlencoded访问API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!