Trying to reach API using Content-Type application/x-www-form-urlencoded

匿名 (未验证) 提交于 2019-12-03 01:39:01

问题:

I am building API restful server using dotnet core framework. I added my controller and trying to reach an endpoint using postman.

I have 2 problems

  • Problem 1

    // 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

  • 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:

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



回答2:

You can use Consumes attribute like that:

// POST api/user [HttpPost, Consumes("application/x-www-form-urlencoded")] [Authorize()] public async Task<IActionResult> Post([FromBody]UserModel user) { } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!