How to send a HTTP header using a cURL

September 10, 2022 No comments curl header http

1. Introduction

In this short article, we are going to show how to prepare an HTTP request with one and many headers using curl.

2. -H --header parameter

According to curl manual page: -H --header parameter sets additional header to provide with the request when communicating through HTTP with a server. Any number of additional headers may be specified.

3. Set single HTTP header with curl

To set a single HTTP header simply use curl with -H or --header parameter with a value in format: [HEADER NAME]:[HEADER VALUE] like in the following example:

curl --header "X-Custom-Header: 123"

In this example, we set one header with X-Custom-Header name and 123 value.

4. Set multiply headers using curl

Use more than one -H --header parameter if you wish to send several headers. The curl will parse each as a distinct header.

curl -H "Accept: application/json" -H "Content-Type: application/json" http://server/endpoint

In this example we will send two headers:

  • Accept: application/json - to notify server we will accept JSON data,
  • Content-Type: application/json - use to inform the server site that we will send data in JSON format.

5. Conclusion

In this short article, we present how to set headers with HTTP requests using the curl tool.

{{ message }}

{{ 'Comments are closed.' | trans }}