88

Curl by default adds headers such as Content-type and User-agent. Normally that is a good thing but I'm trying to test what our server does when those headers are missing.

My problem is with the Content-type header. If it is missing, the server correctly assumes the user sent JSON. However, curl actually adds the missing header and incorrectly assumes that the content I am posting application/x-www-form-urlencoded. It also sends an Accept header of */*.

I suppose that is nice default behavior but I basically would like it to not send headers I did not specify. Is there an option for that?

curl -v -X POST 'https://example.com' -d '{...}'

> User-Agent: curl/7.37.1
> Host: example.com
> Accept: */*
> Content-Length: 299
> Content-Type: application/x-www-form-urlencoded

2 Answers 2

132

Use -H flag with the header you want to remove and no content after the :

-H, --header LINE   Custom header to pass to server (H)

Sample

-H 'User-Agent:'

This will make the request without the User-Agent header (instead of sending it with an empty value)

Sign up to request clarification or add additional context in comments.

6 Comments

For removing proxy headers, replace -H by --proxy-header, e.g. --proxy-header "Proxy-Connection:"
-H 'User-Agent:' removes header as expected (no header and no value will be sent) but be careful with -H 'Content-Type:' which will send header with empty value. It is strange different behaviour of curl command.
Contrary to @mikep's experience, I found that (from libcurl, with version 7.63.0) setting "Content-Type:" as a header did completely remove that header from the request, at least according to a Wireshark capture.
I have curl 7.66.0 (x86_64-pc-cygwin) libcurl/7.66.0 and if I pass -H 'Content-Type:' as parameter it does not send Content-Type header.
One can use -v option passed to curl in order to see what exactly is being sent. Wireshark is a little bit too much for such a check. :) Though it is probably more precise if some tool doesn't report things properly.
|
38

Seems like curl sends 3 headers. To do a request without them you can append the arguments:

-H 'User-Agent:' -H 'Accept:' -H 'Host:'

+1 to @cmlndz answer as he explains how to remove a single header.

You can check which headers are actually sent by adding -v.

10 Comments

If your request has a body (like a POST request) it will also set Content-Length: 433 or whatever the size of the body is. You can use -H 'Content-Length:' to exclude that header (although this likely leads to an invalid request as it should include the length or set the Transfer-Encoding header)
-H 'Host:' will most likely result in a "HTTP Error 400. The request hostname is invalid." You will also need to add the --http1.0 parameter to the curl command.
What has to be done when for example, we need to prevent cURL from adding "Host" header by default yet send our own custom "host" header? Is it ideal to do something like this? -H 'Host:' -H 'host:www.example.com'
@mang4521 I think you can just override it with single -H 'Host:www.example.com'. No need to remove then set. It will probably then just go down to parameter order precedence and be confusing.
@mang4521, that's explained in the answer. To recap, if you want to remove, use what is given in the answer. If you want to change the header, just set it as I stated in my earlier comment. You don't have to assume, just try it out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.