简体   繁体   中英

cURL - Setting Content-Type Header is not working?

I'm trying to set the content-type header of my cURL request to application/json.

This is the request I'm using in cmd (I left out the Json-Body):

curl -v -X POST http://localhost:40071/api/Sale --header 'Content-Type: application/json' -d '{[Json Body]}'

When running this command, cURL is logging the following:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1:40071...
* Connected to localhost (127.0.0.1) port 40071 (#0)
> POST /api/Sale HTTP/1.1
> Host: localhost:40071
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Length: 332
> Content-Type: application/x-www-form-urlencoded
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 500 Internal Server Error
< Date: Tue, 09 Nov 2021 11:18:33 GMT
< Content-length: 0
<

My REST server is running into an exception because of HTTP 415 Unsupported Media Type.

It seems as if cURL did not make use of my "--header" parameter? Because the log is showing that Content-Type: application/x-www-form-urlencoded was chosen.

I also tried it with the abbreviated parameter "-h".

Seems duplicate question, for more examples, refer here.

How to send a header using a HTTP request through a cURL call?

curl -X POST mockbin.org/request -H "Accept: application/json"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM