Http Method Head: Request Method | HTTP HEAD request method
Articles / Top 9 Methods for HTTP / What is HTTP HEAD Method and How to Send HTTP HEAD Requests?
The HTTP HEAD method requests the HTTP headers from the server as if the document was requested using the HTTP GET method. The only difference between HTTP HEAD and GET requests is that for HTTP HEAD, the server only returns headers without a body.
HTTP HEAD is much faster than HTTP GET because much less data is transferred in HEAD requests. Browsers use the HEAD method to update information about cached resources to check if the resource has been modified since it was last accessed. If the resource is not modified, browsers reuse the local copy without issuing a new request. Otherwise, they request an updated version of the resource with the GET request.
Http Method Head: Request Method
The meta information returned in the HTTP headers for a HEAD request must match the meta information returned for the GET request. An HTTP HEAD request is often used to retrieve metadata about a resource at a specific URI without transferring the actual data. For example, to check the availability of hypertext links (check broken links).
Only requests using the HTTP HEAD method should retrieve data (the server should not change its state). If you want to change data on the server, use the POST, PUT, PATCH or DELETE methods. The HTTP HEAD method is defined as passive, which means that multiple identical HEAD requests must have the same effect as a single request.
What is the difference between HTTP HEAD and GET methods?
The HTTP HEAD and GET methods are identical, except for HEAD requests, the server does not return a response body but still determines the size of the response content using the Content-Length header.
HTTP HEAD Request Example
The following HTTP HEAD request example demonstrates sending a HEAD request to the server.
And the server response:
HTTP/1.1 200 OK Content-Length: 19 Content-Type: application/jso
What is the HTTP HEAD request method used?
An HTTP HEAD request is used to check the availability, size, and last modified date of a resource without downloading it (as indicated in the Content-Length and Last-Modified headers).
For example, we can send a GET request to check if a PDF is available for download. The server will return a status code of 200 if the file is available and a status code of 404 if the file is no longer available. The server will also return the contents of the PDF in the response body. If the PDF file is several megabytes in size, we will get unnecessary traffic of up to several megabytes. If the HEAD is requested, we will still get the 200 and 404 status codes, but without the extra megabytes of traffic.
Can I send data using the HTTP HEAD method?
No, HTTP HEAD requests cannot contain a message body. But you can still send data to the server using URL parameters. In this case, you are limited to the maximum URL size, which is around 2000 characters (it depends on the browser).
Can I send HTTP headers using the HEAD method?
Yes, you can send any HTTP headers with a HEAD request, just as you would with a GET request. For example, you can send user authentication data in the authorization header, provide additional information about your server using the X-Powered-By header, or about your user using the X-User-IP header. By default, browsers send accept-, accept-encoding, user-agent, and HTTP headers in every HEAD and GET request.
Some notes on HTTP HEAD requests:
- HEAD requests can be cached
- HEAD requests should never be used when dealing with sensitive data