Remove Internally Generated Headers in Curl

When making HTTP requests using the curl command-line tool or its associated libraries, the utility automatically generates several default headers such as User-Agent, Accept, and Host. This article provides a quick and direct guide on how to disable, remove, or prevent these internally generated headers from being sent in your outgoing curl requests.

Removing Headers in the Curl Command Line

To remove an internally generated header using the curl command-line interface (CLI), use the -H (or --header) option, specify the header name, and follow it immediately with a colon and no value.

For example, curl automatically sends a User-Agent header. To remove it entirely from your request, run:

curl -H "User-Agent:" https://api.example.com

By placing a colon directly after the header name with nothing after it, curl is instructed to omit that specific header from the outgoing request.

Removing Multiple Default Headers

If you need to remove more than one default header, you must use the -H option multiple times, once for each header you wish to exclude.

curl -H "User-Agent:" -H "Accept:" -H "Referer:" https://api.example.com

In this example, the request will be sent without the User-Agent, Accept, or Referer headers.

Removing Headers in PHP cURL

If you are using curl within a programming language like PHP via libcurl, the same logic applies. You pass an array of headers using CURLOPT_HTTPHEADER, defining the header name followed only by a colon.

$ch = curl_init('https://api.example.com');

curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'User-Agent:', // Removes the User-Agent header
    'Accept:'      // Removes the Accept header
]);

curl_exec($ch);
curl_close($ch);

Important Considerations