How to send HTTP header with Curl request? [PHP Code]

To send an HTTP header with a Curl request, you can use the -H command-line option and pass the header name and value in "Key: Value" format. If you do not provide a value for the header, this will remove the standard header that Curl would otherwise send. The number of HTTP headers is unlimited. You can add as many headers to the Curl request as you need. In this Curl header example, we send the X-Custom-Header and Content-Type request headers to the ReqBin echo URL. Click Run to execute the Curl Send Header Request online and see the results. The PHP code was automatically generated for the Curl Send Header example.
How to send HTTP header with Curl request? [PHP Code] Run
curl https://reqbin.com/echo/get/json
   -H "X-Custom-Header: value"
   -H "Content-Type: application/json"
     
Updated: Viewed: 47867 times
PHP code for Curl Send Header example

PHP code for Curl Send Header Example

This PHP code snippet was generated automatically for the Curl Send Header example.
<< Back to the Curl Send Header example

What is Curl?

Curl (stands for Client URL) is a popular command-line tool that you can use to transfer data to/from a server using a range of network protocols, including HTTP, HTTPS, and FTP. Developers can use the Curl project library (libcurl) to make HTTP requests directly from their applications. Curl works on platforms Linux, Windows, and macOS.

What is HTTP Header?

HTTP Headers are key/value pairs that pass additional data between the client and the server. In HTTP, the headers are used to transfer data in both directions. The client can use HTTP headers to send data such as the User-Agent name, the list of supported languages, and the data type in the body of the request message when submitting HTML forms. In turn, the server uses HTTP headers to tell the client the size and type of data in the response body, the compression method used, caching directives, and CORS headers.

Can I send custom HTTP headers using Curl?

Yes, you can add any custom HTTP header to your Curl request using the -H command-line option and send it to the server. Before 2011, it was recommended to name custom HTTP headers with the X- prefix to emphasize that a non-standard HTTP header is being used. However, this recommendation was deprecated in June 2012, and RFC 6648 now specifies that creators of new header parameters SHOULD NOT prefix the header names with “X-” or similar prefixes.

Pass HTTP Header to Curl
curl [URL] -H "Accept: application/json"

Can I send Curl headers with HTTP POST request?

Yes, you can send any number of additional HTTP headers with a POST request. For example, when sending JSON to the server, you can specify the data type in the request body using the Content-Type: application/json header and tell the server that the Curl client is expecting JSON using Accept: application/json". If you do not pass these additional headers with a Curl POST request, the server may misinterpret the request data or return a response in a different format.

Pass HTTP Headers with Curl POST request
curl [URL]
   -H "Content-Type: application/json"
   -H "Accept: application/json"
   -d "[json data]"

Can I only get HTTP headers using Curl?

Yes, the -I or --head Curl command-line option allows you to fetch HTTP headers only from the server by sending a HTTP HEAD request. The HEAD request method is identical to the GET method, except that the server does not return the message body.

Get only HTTP headers using Curl
curl -I https://reqbin.com/echo

Custom Curl Headers Example

An example of sending JSON data to a server with additional Accept and Content-Type HTTP headers.

Curl Headers Example
curl -X POST https://reqbin.com/echo/post/json
   -H 'Accept: application/json'
   -H 'Content-Type: application/json'
   -d '{"id": "12345"}'

How to print request headers using Curl?

You can use the -v or --verbose command-line argument to view the request headers. This will print a lot of debugging information about how Curl makes the request, including the HTTP headers sent to the server and received from the server.

Print the Request Headers using Curl
curl -v https://reqbin.com/echo

How to pass multiple headers to Curl?

To pass multiple headers in a Curl request, use the -H command-line argument as many times as you need.

Pass Multiple Headers to Curl
curl https://reqbin.com/echo/get/json
   -H "X-Custom-Header: value"
   -H "Content-Type: application/json"

How can I remove the standard request header from a Curl request?

To remove the default HTTP header from the Curl request, such as the User-Agent name, add the HTTP header name with no value using the -H command-line option.

Remove Request Header from Curl
curl -H "User-Agent:" https://reqbin.com/echo

How to print server response headers using Curl?

The -i or --include command-line argument tells Curl to print HTTP response headers in its output. HTTP response headers usually contain information such as the MIME type and size of the resource in the HTTP message body, a list of cookies, caching instructions, etc.

Print the Response Headers using Curl
curl -i https://reqbin.com/echo

See also

Generate code snippets for PHP and other programming languages

Convert your Curl Send Header request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.