问题
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