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: 20569 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 HTTP HEAD?

HTTP HEAD is one of 9 standard request methods supported by the HTTP protocol. For HEAD requests, the server sends a response that is identical to the GET request but without the response body. HEAD requests are used to get meta-information about a resource, such as a type and size of the resource. Since the server does not return a resource body for a HEAD request (as opposed to a GET request), this makes a HEAD request an ideal method for checking a page for broken links. The HTTP HEAD method must be read-only (the server must not change its 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. The HEAD method is defined so that the server should return headers in the same way as for a GET request but without a body. This means that you can see the Content-Type and Content-Length headers in the server response, but the response will not contain the message body itself.

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]

Where:
  • -I, --head: tells the Curl to fetch HTTP headers only.

Curl HEAD Request Examples

The following are examples of sending HEAD requests:

Curl HEAD request using -I parameter

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

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

Curl HEAD request using --head parameter

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

Curl HEAD rRequest with --head Example
curl --head https://reqbin.com/echo

Curl 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 https://reqbin.com/echo

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

Curl recommends using the -I method. 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.

// try get IpInfo ASAP! getIpInfo($.noop);