What Is the Correct Way to Submit Content-Type Header in Curl?

I’m using Mac 10.9.5 with bash shell. I’m trying to submit a request via curl, but I keep getting errors when I try and set the content-type header. Below I try

davea$ curl -v -o -H "Content-Type: application/json" -X POST -d '{"username”:”username”,”password”:”password”}’ 

but curl spits back a “curl: (6) Couldn't resolve host 'Content-Type'” error. Below is the complete output:

* getaddrinfo(3) failed for Content-Type:80
* Couldn't resolve host 'Content-Type'
* Closing connection 0
curl: (6) Couldn't resolve host 'Content-Type'
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#1)
> POST /myproject/login HTTP/1.1
> User-Agent: curl/7.40.0
> Host: localhost:8080
> Accept: */*
> Content-Length: 40
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 40 out of 40 bytes
< HTTP/1.1 302 Found
< Server: Apache-Coyote/1.1
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Set-Cookie: JSESSIONID=B980765C84EA5759F743D1AAE8E189D0; Path=/myproject/; HttpOnly
< Location: 
< Content-Length: 0
< Date: Mon, 06 Jul 2015 16:03:37 GMT
< 
* Connection #1 to host localhost left intact

What is the correct way to submit the content-type header via curl?

1 Answer

What is the correct way to submit the content-type header via curl?

Using the -H parameter, as you specify:

-H "Content-Type: application/json"

On the other hand, you have also specified the -o (output to file) option, without specifying a file:

If you want a progress meter for HTTP POST or PUT requests, you need to redirect the response output to a file, using shell redirect (>), -o [file] or similar.

(from man curl)

So the command becomes:

$ curl -o output.txt -H "Content-Type: application/json" -X POST -d '{"username":"username","password":"password"}' 

(NB I have also replaced smart quotes in the above command as they have made their way into your question)

Should submit the header and output (to output.txt) as you specify. You could also leave off the -o output.txt parameter if you do not require that. Although the man curl page doesn't seem to specify it, in testing -v cannot be mixed with -o.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest