0

cURL adds certain default request headers into the request made such as Host, Accept-Language etc.

As per How can I remove default headers that cURL sends? in order to remove these default headers we need to do something like this:

-H 'Host:'

I would like to send the following headers [in the given order]

curl -H 'accept:*/*' -H 'host:www.example.com'

But due to curl adding its default headers, I see this

Host: www.example.com
accept: */*

How do we ensure the default headers are not added here?

Also, what if we need to remove the default header but replace it with a more customised header [say 'Host' to 'host']? What are we supposed to do? I am thinking on these lines

-H 'Host:' -H 'host:www.example.com'

Is this the right way to handle this requirement? If it is, is it the ideal way? Are there other ways to remove default headers added by cURL?

2
  • HTTP headers are case-insensitive, so Host: and host: are the same header. Why do you need to force it to lower-case? Commented Sep 27, 2022 at 1:11
  • That is just an example. What if I want to change the order of headers? Kindly check the edited content. Commented Sep 27, 2022 at 4:21

1 Answer 1

0

I do not think you can overwrite the host: header.

I tried many different capitalizations of HOST and it made no difference.

I looked at both the out going request hearer sent by curl and the request hearer received by the server.

It depends on the type of request being made what the default headers will be. I use PHP curl so the defaults are stored in my php.ini.

For example if I do this curl:

$ch = curl_init('http://eatled.com/receiveheader.php');
$response = curl_exec($ch);

The request header is

Accept: */*
Host: eatled.com  

You can use this URL http://eatled.com/receiveheader.php and it will return some info on your request, including the request headers.

If I change the request to a POST,

$ch = curl_init('http://eatled.com/receiveheader.php');
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);

I get different request headers.

Content-Type: application/x-www-form-urlencoded
Accept: */*
Expect: 100-continue
Host: eatled.com
Transfer-Encoding: chunked

If I do this GET request from a Windows command line

curl http://eatled.com/receiveheader.php

I get these headers:

Accept: */*
Host: eatled.com
User-Agent: curl/7.83.1

If I do this POST in Windows

curl http://eatled.com/receiveheader.php -X POST

I get the same response.

Accept: */*
Host: eatled.com
User-Agent: curl/7.83.1
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like we can override the Host header [as far as my tests go]. This does the trick -H 'Host:' -H 'host:www.example.com'. But I am unsure if this is the right way to do it or not.

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.