Curl With Headers

Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Published: August 3, 2023

A “curl request header” is an HTTP header that can be used in an HTTP request to provide additional context and metadata, so that the server can tailor the way it processes the request and sends a response.

The short answer

To set a single header when sending a request with curl, you can use the -H or --header flag as follows:

Bash
$ curl -H "<header>" URL

Where <header> is an HTTP header composed of a name and a value in the following format: name: value. For example: "Content-Type: plain/text".

curl with multiple headers at once

To pass multiple headers at once with curl, you can simply repeat the -H flag for each header you want to add to the HTTP request as follows:

Bash
$ curl -H "<header1>" -H "<header2>" URL

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering curl send headers json token in the AI Command Search will prompt a curl command that includes the Content-Type and the Authorization headers, which can then quickly be inserted into your shell by doing CMD+ENTER.

Examples of common use cases for curl headers

Here are a few examples of commonly used HTTP headers when sending requests with curl.

Sending authenticated requests

Since many APIs require requests to be authenticated, to access protected data or perform sensitive operations, you can use the Authorization header to send your credentials alongside with the request itself.

Here is an example of a curl GET request with an authorization header:

Bash
$ curl -H "Authorization: Bearer <token>" 
  https://api.example.com/user/profile

You can also read our other articles if you want to learn more about sending authentication headers and performing basic authentication with curl.

Specifying the payload encoding of the request

Another common use case for sending headers with curl is to specify the encoding of the payload contained in the request. This is useful when sending data in a specific format such as JSON or URL-encoded, to indicate to the server which parsing method to use.

Here is an example of a curl POST request with a content-type header:

Bash
$ curl -X POST -H "Content-Type: application/json" 
  -d '{"city":"paris"}' https://api.weather.com/forecast

Specifying client requirements for the response

Here's a more complex example with multiple headers used to indicate to the server how it should format its response before sending it back to the client.

Bash
$ curl -H "Accept: application/json" -H "Accept-Language: en-US" 
  -H "Cache-Control: max-age=3600" https://api.example.com

Where:

  • The "Accept" header indicates that the response should be in the JSON format.
  • The "Accept-Language" header indicates that the response should be in American English.
  • The "Cache-Control" header indicates that the response should not be older than 3600 seconds (i.e. 1 hour).
Written by
Razvan Ludosanu
Razvan LudosanuFounder, learnbackend.dev
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Curl Post Request

Use cURL to send data to a server

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Create Directories Recursively With mkdir

Learn how to recursively create nested directories using the mkdir command, Bash scripts, and Python scripts.

Remover Users in Linux

Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.