DEV Community

Cover image for Mastering HTTP HEAD Requests with curl: Developer’s Guide to Efficiency & Automation
Habibur Rahman
Habibur Rahman

Posted on • Edited on

Mastering HTTP HEAD Requests with curl: Developer’s Guide to Efficiency & Automation

The curl command is one of the most widely used tools for making HTTP requests from the command line. It is an essential utility for developers, system administrators, and security analysts. One of the lesser-used but highly valuable options in curl is the HEAD request. In this article, we will dive deep into what a HEAD request is, how to use curl to make HEAD requests, and explore various use cases, examples, and best practices.

Check out ApyHub for game-changing solutions that simplify your development process.

What is a HEAD Request?

In HTTP, the HEAD method is similar to the GET method, but it does not return the body of the response. Instead, it retrieves only the headers of the response. This is useful when you want to check metadata, such as content type, response status, or caching details, without downloading the actual content.

Difference Between HEAD and GET Requests

HEAD Request: Returns only headers and does not include the response body.

GET Request: Retrieves both headers and the full response body.

Using a HEAD request reduces bandwidth usage and speeds up the process of obtaining information about a resource without downloading it.

How to Use curl to Make a HEAD Request

The curl command provides an option -I (uppercase i) to send a HEAD request. The basic syntax is:

curl -I <URL>

Enter fullscreen mode Exit fullscreen mode

Example: Checking Headers of a Website

To retrieve the headers of a webpage, you can run:

curl -I https://www.example.com

Enter fullscreen mode Exit fullscreen mode

This will return a response similar to:

HTTP/2 200
server: Apache
content-type: text/html; charset=UTF-8
content-length: 12345
date: Wed, 05 Mar 2025 10:00:00 GMT
Enter fullscreen mode Exit fullscreen mode

Understanding the Response Headers

Each line in the response headers provides valuable information. Here are some common headers:

HTTP Status Code: Indicates the status of the request (e.g., 200 OK, 404 Not Found, 301 Moved Permanently).

Server: Specifies the web server software being used (e.g., Apache, Nginx).

Content-Type: Indicates the MIME type of the resource (e.g., text/html, application/json).

Content-Length: Shows the size of the response body in bytes.

Date: The date and time when the response was generated.

Cache-Control: Specifies caching policies for the resource.

Location: If a redirect is involved, this header provides the new URL.

Using curl HEAD Request with Additional Options

  1. Follow Redirects

Sometimes, a HEAD request may result in a 301 or 302 redirect. By default, curl does not follow redirects unless instructed to do so.

To follow redirects, use the -L option:

curl -I -L https://example.com

  1. Custom User-Agent

Web servers often block curl requests if they detect an unusual user agent. You can specify a user-agent string using the -A option:

curl -I -A "Mozilla/5.0" https://example.com

Enter fullscreen mode Exit fullscreen mode
  1. Checking Only Specific Headers

If you are interested in only specific headers, use grep to filter the output:

curl -I https://example.com | grep "Content-Type"

  1. Sending a HEAD Request to an API

Many APIs support the HEAD method for checking availability and metadata. Example:

curl -I https://api.example.com/v1/data

Practical Use Cases of curl HEAD Requests

  1. Checking if a URL is Reachable

Before making a full GET request, you can use HEAD to check if a URL is live.

curl -I https://example.com | head -n 1

If the response contains HTTP/1.1 200 OK, the site is accessible.

  1. Validating Redirects

If a webpage has moved, a HEAD request helps check the redirection location.

curl -I -L https://oldsite.com

  1. Verifying API Response Headers

When working with APIs, you may need to inspect headers to check authentication requirements, content type, or rate limits.

curl -I -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/v1/status

  1. Checking Content-Type Before Downloading

To ensure a file is of the expected type before downloading:

curl -I https://example.com/file.zip | grep "Content-Type"

  1. Debugging Website Issues If a website is slow or not responding as expected, analyzing response headers can provide insights into server configuration, caching, and security settings.

Best Practices for Using curl HEAD Requests

Avoid Overuse: Sending frequent HEAD requests can put unnecessary load on the server.

Use with APIs Wisely: Not all APIs support HEAD requests; always check the API documentation.

Combine with Other Tools: Use grep, awk, or jq for parsing and analyzing headers.

Follow Redirects If Needed: Some URLs may have multiple redirects before reaching the final destination.

Check Rate Limits: Some websites and APIs impose request limits; be mindful of them.

Conclusion

The curl HEAD request is a powerful and efficient way to retrieve HTTP headers without downloading the response body. Whether you are a developer debugging API headers, a security analyst verifying response metadata, or a system administrator checking website reachability, mastering curl -I will significantly improve your workflow.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay