Python code for Curl HEAD Request ExampleThis 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 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:
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:
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.
In response to such a request, Curl will display the response with the full list of received headers:
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.