尝试使用 Content-Type application/x-www-form-urlencoded 访问 API [英] Trying to reach API using Content-Type application/x-www-form-urlencoded

查看:31
本文介绍了尝试使用 Content-Type application/x-www-form-urlencoded 访问 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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.

我有两个问题

  • 问题 1

// POST api/user
[HttpPost]
[Authorize()]
 public async Task<IActionResult> Post([FromBody]UserModel user)
{
}

除非我通过键入原始 json 将邮递员的请求作为 application/json 发送,否则如果我使用 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

// 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.至于第二,我认为编写简单的单元测试或使用 Swashbuckle,它有很好的接口来处理这样的请求

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 application/x-www-form-urlencoded 访问 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆