# using the HTTP HEAD method

> How to request information about an HTTP resource without downloading its body

- Autore / Author: Diego Betto (https://diegobetto.com/about/)
- Pubblicato / Published: 2023-10-18
- URL: https://diegobetto.com/en/http-head-method/
- Tag: http, head, fetch, request, method

The **HEAD method** is an [HTTP](https://httpwg.org/specs/) method that lets you get a resource's header information without returning the resource's body itself. It's similar to the **GET method**, but doesn't require transmitting the response body.

## Benefits of using the HEAD method

The HEAD method has several advantages over the GET method, including:

- **Saves bandwidth:** the server doesn't have to send the resource's body, so the request needs less bandwidth.
- **Improves performance:** the request takes less time to complete.
- **Can be used to check for a resource's existence:** if the response to the HEAD method has an HTTP 200 status code, it means the resource exists.

## Example of using the HEAD method in JavaScript

Here's an example of using the HEAD method in JavaScript:

```js
const response = await fetch("https://www.example.com/", {
  method: "HEAD",
});

// Check the HTTP status code
if (response.status === 200) {
  // The resource exists
} else {
  // The resource doesn't exist
}
```

In this example, the request is sent to the website "www.example.com". The response's HTTP status code indicates whether the resource exists or not.

## Other uses for the HEAD method

The HEAD method can be used in various ways, for example:

- **To check a file's last modified date:** the `Last-Modified` header field in the response contains the date and time of the file's last modification.
- **To check a file's size:** the `Content-Length` header field in the response contains the file's size.
- **To check if a resource requires authentication:** the `WWW-Authenticate` header field in the response contains the information needed to authenticate.

## Conclusion

The HEAD method is a useful tool for getting information about a resource without having to fully download it. It's an effective method for saving bandwidth, improving performance, and checking whether a resource exists.

### Resources

- [MDN Web Docs: HTTP HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD)
