Curl 415 Unsupported Media Type

廉价感情. 提交于 2020-05-30 09:21:44

问题


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

Response:

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

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. I am using Windows 10 and git bash to run the script.


回答1:


Unsupported Media Type

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.

Please try to specify the header to --header 'Content-Type: multipart/form-data', like below.



来源:https://stackoverflow.com/questions/60207198/curl-415-unsupported-media-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!