
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:
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 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 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:
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.