我有一个带有此帖子端点的 ASP.NET Core Web API 项目:
[HttpPost("post")]
public async Task<ActionResult<Player>> PostPlayer(Player player)
{
_context.Players.Add(player);
await _context.SaveChangesAsync();
return CreatedAtAction("GetPlayer", new { id = player.Id }, player);
}
我可以使用此 JSON 负载在 Swagger 中向该端点发出发布请求:
{
"handle": "Cole",
"role": "Fixer",
"avatar": "null"
}
在这个地址:https://localhost:44326/api/Players/post
。
它成功地创建了一个新的播放器。
但是,在 Postman 中使用相同的地址和相同的 JSON 有效负载作为正文,我得到了这个:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.13",
"title": "Unsupported Media Type",
"status": 415,
"traceId": "00-36aa0c0b65a981479a1bb99d32400eff-77eb26e1d840de4d-00"
}
这到底是什么意思?为什么这种 post 方法在 Postman 中不起作用?我所有的 Get 端点在 Postman 中都可以正常工作,只是不是这个。
如果您的请求正文是 JSON,您需要确保在 Postman 的正文选项卡中选择了正确的标头和正确的请求类型。
Content-Type
标题设置为application/json
(如果不存在则添加)。如果上述步骤不起作用,那么最好的方法是将工作请求(来自 Swagger)与 Postman 请求进行比较。您可以这样做:
Content-Type
标头传递的值。一旦您在“网络”选项卡中跟踪了一个工作请求,就应该很容易弄清楚您的 Postman 请求与 Swagger 请求的不同之处。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句