• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

Curl With Headers

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Published: 8/3/2023

About Terminus

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:

 $ curl -H "<header>" URL

Run in Warp

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:

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

Run in Warp

Easily retrieve this command using Warp’s AI Command Search

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

Thumbnail for

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:

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

Run in Warp

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:

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

Run in Warp

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.

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

Run in Warp

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

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Filed Under

Related Articles

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.

UnixLinux
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

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.

LinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

Linux Chmod Command

Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.

Linux
Thumbnail for Razvan LudosanuRazvan Ludosanu

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

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.

Linux

Create Groups In Linux

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

Linux

Switch Users In Linux

Learn how to switch between users, log in as another user, and execute commands as another user in Linux.

Linux

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.

Linux

Delete Files In Linux

Learn how to selectively delete files in Linux based on patterns and properties using the rm command.

Linux

Find Files In Linux

Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.

Linux

Copy Files In Linux

Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.

Linux

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Thumbnail for null