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
// 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
// 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. As for second, i think will be easier write simple unit test or use Swashbuckle, it has great interface for such requests
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)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With