Curl 415不支持的媒体类型

克拉姆

我试图用curl测试我的端点并得到415:

curl -X POST "http://localhost:5001/api/countries/import" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer "$API_TOKEN \
--data @/D:/_countries.json

响应:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"0HLTFOME7T990:00000001"}

这是我的.net核心终结点:

// POST api/countries/import
[HttpPost]
[Route("[action]")]
public async Task<IActionResult> Import(IFormFile file)
{
    ...
}

顺便说一句,我在邮递员中使用此端点没有问题,尽管它生成的代码对我不起作用(响应是相同的):

curl --location --request POST 'http://localhost:5001/api/countries/import' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {token here}' \
--form 'file=@/D:/_countries.json'

PS我正在使用Windows 10和git bash运行脚本。

Fei Han

不支持的媒体类型

您的动作public async Task<IActionResult> Import(IFormFile file)期望IFormFile参数,但是您使用分隔了请求标头Content-Type: application/json,从而导致了此问题。

请尝试将标头指定为--header 'Content-Type: multipart/form-data',如下所示。

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章