How do I send a HEAD request using Curl? [Java Code]
To make a 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 HEAD request online and see the results. The Java code was automatically generated for the Curl HEAD Request example.
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?
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]
-I, --head: tells the Curl to fetch HTTP headers only.
Curl HEAD Request Examples
Three examples of sending a HEAD request to the ReqBin echo URL using the -I, --head, and -X HEAD command-line options.
Curl HEAD request with -I parameter
curl -I https://reqbin.com/echo
Curl HEAD request with --head parameter
curl --head https://reqbin.com/echo
Curl HEAD request with -X HEAD parameter
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.
Generate code snippets for Java and other programming languages