Sending HTTP HEAD Request with Curl [Python Code]

To make an HTTP HEAD request with Curl, you need to use the -I or --head command-line parameter. The -I command-line parameter tells Curl to send an HTTP HEAD request to receive only HTTP headers. The HEAD request is very similar to a GET request, except that the server only returns HTTP headers without a response body. In this Curl HEAD request example, we send a HEAD request to the ReqBin echo URL. Click Run to execute the Curl HTTP HEAD Request Example online and see the results. The Python code was automatically generated for the Curl HEAD Request example.
Sending HTTP HEAD Request with Curl [Python Code] Run
curl -I https://reqbin.com/echo
Updated: Viewed: 39313 times
Python code for Curl HEAD Request example

Python code for Curl HEAD Request Example

This Python code snippet was generated automatically for the Curl HEAD Request example.
<< Back to the Curl HEAD Request example

What is Curl?

Curl is an open-source command-line tool and cross-platform library (libcurl) for transferring data between clients and servers that run on almost all platforms and hardware. Curl supports all popular Internet protocols and is used wherever you need to send or receive data over the network.

What is the HTTP HEAD Method?

HTTP HEAD is one of 9 standard request methods supported by the HTTP protocol. For HEAD requests, the server sends a response identical to the GET request but without the response body. HEAD requests are used to get meta information about a resource, such as the type and size of the resource. The HTTP HEAD method should not change the server state and must be idempotent, which means that multiple identical HEAD requests must have the same effect as a single request.

Sending an HTTP HEAD request with Curl

To send a HEAD request using Curl, you must pass the --head (-I) parameter to the Curl call. An alternative way to send a HEAD request using Curl is to pass the -X HEAD command-line argument instead of -I. Please note that some servers may reject HEAD requests but still respond to GET requests.

Curl HEAD Request Syntax

The general form of the Curl command for sending a HEAD request is as follows:

Curl HEAD Request Syntax
curl -I [URL]

Curl HEAD Request Examples

The following are examples of sending HEAD requests with Curl:

Basic Curl HEAD request example

To make a Basic HEAD HTTP request with Curl, you can use the -I or --head command line option:

Basic Curl HEAD request Example
curl -I https://reqbin.com/echo

curl --head https://reqbin.com/echo

Sending a HEAD request using -X HEAD parameter

The following is an example of sending a HEAD request to the ReqBin echo URL using the -X HEAD command line option:

Curl HEAD Request with -X HEAD Example
curl -X HEAD -i https://reqbin.com/echo

How to see server response headers in Curl HEAD requests?

To view the server response headers in a HEAD Curl request, you can use the -I or --head option. The -I or --head flag returns only the response headers from the server, omitting the content itself. This can be especially useful when you need information about a resource on the server, such as content type, last modified date, file size, and other metadata, without getting the actual data.

Curl HEAD Request with -I Example
curl -I https://reqbin.com/echo

In response to such a request, Curl will display the response with the full list of received headers:

Server Response Headers Example
HTTP/1.1 200 OK
Date: Wed, 26 Aug 2023 12:34:56 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 12345
...

Which method is better to use -I or -X HEAD?

Both methods achieve the same result of fetching headers without downloading the content. Curl recommends using the -I method. The -I option with the curl command sends a HEAD request to the specified URL. The -X HEAD method does not display headers by default, and to view the headers when using the -X HEAD method, you also need to pass the -i (lowercase) command line parameter to Curl. Hence -I is the correct way to get headers.

See also

Generate code snippets for Python and other programming languages

Convert your Curl HEAD Request request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Python code generator.