When making HTTP requests using cURL, it is often useful to view the sent request headers to debug or better understand the exchange of data between the client and server. HTTP headers contain metadata about the request, like the client's user agent, accepted content types, and cookies being sent.
Understanding request headers is essential when diagnosing potential issues, simulating specific client behaviors, or verifying that the right parameters are being passed to the server. By default, cURL only displays the response body in the terminal. However, cURL provides mechanisms to view both the request headers sent to the server and the response headers received from the server.
Displaying the request headers while making HTTP requests with cURL offers deeper insight into how servers might interpret and handle your requests. It's also a handy tool for web development, system administration, and security assessments.
$ curl http://www.simplified.guide <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Simplified Guide </title> ##### snipped
$ curl -v http://www.simplified.guide * Trying 127.0.0.1:80... * Connected to www.simplified.guide (127.0.0.1) port 80 (#0) > GET / HTTP/1.1 > Host: www.simplified.guide > User-Agent: curl/8.1.2 > Accept: */* > < HTTP/1.1 200 OK < Date: Tue, 26 Sep 2023 09:00:46 GMT < Server: Apache < X-Powered-By: PHP/8.2.10 < Vary: Cookie < Set-Cookie: DokuWiki=ib5a5d9nf6i398lc0t71f0cjv3; path=/; HttpOnly < Expires: Thu, 19 Nov 1981 08:52:00 GMT < Cache-Control: no-store, no-cache, must-revalidate < Pragma: no-cache < Set-Cookie: DW68700bfd16c2027de7de74a5a8202a6f=deleted; expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0; path=/; HttpOnly < Transfer-Encoding: chunked < Content-Type: text/html; charset=utf-8 < <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> Simplified Guide </title>
Lines starting with > are the request headers.
$ curl -v http://www.simplified.guide > /dev/null 2>&1 | grep '^>' > GET / HTTP/1.1 > Host: www.simplified.guide > User-Agent: curl/8.1.2 > Accept: */* >
This command discards the actual response and filters only the request headers using the grep command.
$ curl -v -H "X-Custom-Header: TestValue" -X POST http://www.simplified.guide > /dev/null 2>&1 | grep '^>' > POST / HTTP/1.1 > Host: www.simplified.guide > User-Agent: curl/8.1.2 > Accept: */* > X-Custom-Header: TestValue
The -H option allows adding custom headers, while -X specifies the HTTP method to use.
$ curl -v -H "X-Custom-Header: TestValue" -X POST http://www.simplified.guide > /dev/null 2>&1 | grep '^>' > headers-output.txt
cat headers-output.txt > POST / HTTP/1.1 > Host: www.simplified.guide > User-Agent: curl/8.1.2 > Accept: */* > X-Custom-Header: TestValue >
Comment anonymously. Login not required.