简体   繁体   English

尝试使用Content-Type application / x-www-form-urlencoded来访问API

[英]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个问题

  • Problem 1 问题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 除非我通过键入raw json从postman发送请求作为application/json ,否则如果我使用application/x-www-form-urlencoded而无法访问此端点,那么我总是得到415不支持的媒体类型

  • Problem 2 问题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 我不知道如何使用邮递员达到这样的终点

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.

相关问题 C# HttpClient REST 请求使用 Content-Type: application/x-www-form-urlencoded - C# HttpClient REST request using Content-Type: application/x-www-form-urlencoded 使用 application/x-www-form-urlencoded 内容类型处理请求,但 NET Core 中的 XML 主体 Web API - Handle Request with application/x-www-form-urlencoded content-type but XML body in NET Core Web API 如何以Content-Type的形式返回响应:application / x-www-form-urlencoded; 从WCF服务? - How to return response as Content-Type: application/x-www-form-urlencoded; from WCF service? RestSharp在POST上将Content-Type默认为application / x-www-form-urlencoded - RestSharp defaulting Content-Type to application/x-www-form-urlencoded on POST 无法使用 Headers[“Content-Type”] = “application/x-www-form-urlencoded” 在我的 WebClient UploadString 中编码我的 JSON 对象 - Unable to encode my JSON object inside my WebClient UploadString using Headers[“Content-Type”] = “application/x-www-form-urlencoded” 在WCF ReSTful服务中,POST或PUT请求的Content-Type是否需要为“ application / x-www-form-urlencoded”? - Does the Content-Type of a POST or PUT request need to be 'application/x-www-form-urlencoded' in a WCF ReSTful service? 使用内容类型application / x-www-form-urlencoded解析从HTTP POST接收的C#中的JSON数据 - Parsing JSON data in C# received from HTTP POST with content-type application/x-www-form-urlencoded 如何使用 HTTPclient content type = application/x-www-form-urlencoded POST - How to POST using HTTPclient content type = application/x-www-form-urlencoded JSON POST,其内容类型为x-www-form-urlencoded的数组参数 - JSON POST with an array parameter with content-type x-www-form-urlencoded 如何在内容类型为x-www-form-urlencoded的C#中发布请求? - How to POST request in c# with content-type being x-www-form-urlencoded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM