How do I add a header to a Curl request? [Curl/Bash Code]
To add a header to the Curl request, you need to use the -H command-line option and pass the name and value of the HTTP header in the following format: "Key: Value". 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. The Curl/Bash code was automatically generated for the Curl Add Header example.
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.
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.
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
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.