Curl 415不支持的媒体类型 [英] Curl 415 Unsupported Media Type
问题描述
我正在尝试用curl测试我的端点并得到415:
I am trying to test my endpoint with curl and getting a 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核心终结点:
And here's my .net core endpoint:
// POST api/countries/import
[HttpPost]
[Route("[action]")]
public async Task<IActionResult> Import(IFormFile file)
{
...
}
顺便说一句,我在邮递员中使用此端点没有问题,尽管它生成的代码对我不起作用(响应是相同的):
Btw, I have no problem with this endpoint in postman although it's generated code doesn't work for me (the response is the same):
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'
P.S.我正在使用Windows 10和git bash运行脚本.
P.S. I am using Windows 10 and git bash to run the script.
推荐答案
不支持的媒体类型
Unsupported Media Type
您的操作public async Task<IActionResult> Import(IFormFile file)
期望使用IFormFile
参数,但是您使用Content-Type: application/json
分隔了请求标头,从而导致了此问题.
Your action public async Task<IActionResult> Import(IFormFile file)
expect IFormFile
parameter, but you sepecified request header with Content-Type: application/json
, which cuase this issue.
请尝试将标题指定为--header 'Content-Type: multipart/form-data'
,如下所示.
Please try to specify the header to --header 'Content-Type: multipart/form-data'
, like below.
这篇关于Curl 415不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!