Curl Headers Example – How to Set/View Headers

In this tutorial, you will learn how to set and view curl headers with practical examples. When using curl, a command-line tool for transferring data with URLs, setting and viewing headers can be crucial for testing APIs, debugging, and simulating different web requests.

Curl Headers Example

Table of Contents


Syntax and Explanation

To add a custom header to a curl request, use the -H or --header flag followed by the header key-value pair. To view the headers, use the -i, --include (to include the response headers in the output), or -D, --dump-header (to save the response headers to a file).

  // Syntax to set a header
  curl -H "Header-Key: Header-Value" [URL]
  // Syntax to view response headers
  curl -i [URL]
  // Syntax to save response headers to a file
  curl -D headers.txt [URL]


Practical Examples

To understand how to work with headers in curl, let’s delve into some examples. In these examples, we will actively demonstrate how to manipulate headers effectively, providing a practical grasp of the concept. So, let’s get started with our exploration of header management in curl.

Example 1: Setting a Single Header

  curl -H "Content-Type: application/json"


{ "status": "success", "message": "Content type set to application/json" }

In this example, we’re specifying the Content-Type header to indicate that the data being sent is in JSON format.


Example 2: Setting Multiple CURL Headers

In this example, we demonstrate how to simultaneously set two headers, a common requirement when interacting with APIs that demand authentication. This active approach to header configuration allows developers to efficiently manage authentication tokens and other necessary information for seamless API integration. By following this example, users can quickly grasp the essential technique of setting multiple headers concurrently in their curl requests.

  curl -H "Content-Type: application/json" -H "Authorization: Bearer YourToken"


{ "status": "authorized", "message": "Multiple headers set successfully." }


Example 3: Viewing Response Headers

  curl -i


  HTTP/1.1 200 OK
  Content-Type: application/json
  Set-Cookie: sessionId=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT

By using the -i flag, this example returns the response body along with the headers, allowing you to view details such as cookies and content types.


Example 4: Saving Response Headers to a File

  curl -D headers.txt

Output in “headers.txt”:

  HTTP/1.1 200 OK
  Content-Type: application/json
  Set-Cookie: sessionId=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT

Instead of displaying them in the console, the -D flag saves the headers into the specified file



Understanding how to set and display headers in curl is vital for web development and API testing. Throughout this tutorial, we’ve covered the syntax and provided clear, actionable examples of setting and viewing headers. Armed with this knowledge, you should be able to integrate headers as necessary in your next development project or while troubleshooting APIs.