Document Moved

How can I see the request headers made by curl when sending a request to the server?

I want to see the request headers made by curl when I am sending a request to the server. How can I check that?

15 Answers 15

I think curl —verbose/-v is the easiest. It will spit out the request headers (lines prefixed with ‘>’) without having to write to a file:

$ curl -v -I -H "Testing: Test header so you see this works" http://stackoverflow.com/ * About to connect() to stackoverflow.com port 80 (#0) * Trying 69.59.196.211. connected * Connected to stackoverflow.com (69.59.196.211) port 80 (#0) > HEAD / HTTP/1.1 > User-Agent: curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/0.9.8h zlib/1.2.3 libssh2/0.15-CVS > Host: stackoverflow.com > Accept: */* > Testing: Test header so you see this works > < HTTP/1.0 200 OK . 

curl -v -D - stackoverflow.com -o /dev/null (in order to do not display whole site's content, just headers)

curl -Ivs http://example.com > /dev/null : -I for a HEAD request, -v to show sent headers, -s to hide progress bar, > /dev/null to show only the -v output, avoiding duplication.

@PiotrekDe -D - was neat, but was not useful to me as it simply duplicated the headers that -v already displayed. If you want them unprefixed for automated machine consumption, then maybe it would be better in that case, but I only wanted to glimpse at what a problem was in more detail.

There are two very helpful feature of the "verbose" flag: first, it also prints the TLS handshake process when accessing website through HTTPS, such as curl -v https://www.example.com ; second, it also prints the CONNECT request if you are visiting the site through HTTP proxy, such as curl --proxy my-proxy:8080 http://www.example.com . I believe it would help more users if some examples of these two features are mentioned in this answer.

TL;DR: Don't use -I In the modern world, when people ask about seeing headers, they are probably talking about APIs. And if you use that "I use -I to see the headers with my Apache webserver" mentality, you are going to waste a lot of time developing against a HEAD method when you probably mean to use GET . Stop telling people to use -I . If they want HEAD , use -X HEAD (TWSS)

Читайте также:  Device drivers programing in linux

The question did not specify if command line command named curl was meant or the whole cURL library.

The following PHP code using cURL library uses first parameter as HTTP method (e.g. "GET", "POST", "OPTIONS") and second parameter as URL.

 $argv[1], CURLOPT_URL => $argv[2], CURLOPT_RETURNTRANSFER => 1, CURLOPT_FOLLOWLOCATION => 0, CURLOPT_VERBOSE => 1, CURLOPT_HEADER => 0, CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 30, CURLOPT_STDERR => $f, )); $response = curl_exec($ch); fseek($f, 0); echo fread($f, 32*1024); # output up to 32 KB cURL verbose log fclose($f); curl_close($ch); echo $response; 
php curl-test.php OPTIONS https://google.com 

Note that the results are nearly identical to following command line

curl -v -s -o - -X OPTIONS https://google.com 

This is the best answer, because the file used in this method contains everything in curl_getinfo() referenced below, along with more details on both the request and the response.

Bear in mind that if the connection is refused files that were specified with CURLOPT_WRITEHEADER and CURLOPT_FILE are empty.

Ive upvoted your answer because while the question was not php related, your php based answer pointed me into the right direction for solving my own issue with sending a bearer token. Thank you. I stated my reason here only in the vain attempt to have this question show up in future google searches for php devs with a similar issue.

The --trace-ascii option to curl will show the request headers, as well as the response headers and response body.

curl --trace-ascii curl.trace http://www.google.com/ 

produces a file curl.trace that starts as follows:

== Info: About to connect() to www.google.com port 80 (#0) == Info: Trying 209.85.229.104. == Info: connected == Info: Connected to www.google.com (209.85.229.104) port 80 (#0) => Send header, 145 bytes (0x91) 0000: GET / HTTP/1.1 0010: User-Agent: curl/7.16.3 (powerpc-apple-darwin9.0) libcurl/7.16.3 0050: OpenSSL/0.9.7l zlib/1.2.3 006c: Host: www.google.com 0082: Accept: */* 008f: 

It also got a response (a 302 response, to be precise but irrelevant) which was logged.

If you only want to save the response headers, use the --dump-header option:

curl -D file url curl --dump-header file url 

If you need more information about the options available, use curl --help | less (it produces a couple hundred lines of output but mentions a lot of options). Or find the manual page where there is more explanation of what the options mean.

-D gives you the response headers (as does -I, but to STDIN). The question asked for the request headers.

Читайте также:  Узнать версию amd драйвера linux

The only way I managed to see my outgoing headers (curl with php) was using the following options:

curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLINFO_HEADER_OUT, true); 
$data = curl_exec($ch); var_dump($data); var_dump(curl_getinfo($ch)); 

var_dump $data will return the response headers as well as the response body. The var_dump curl_getinfo($ch) will give you the request headers.

CURLOPT_HEADER is the response header - CURLINFO_HEADER_OUT is the request header. This is what the OP is asking for 🙂

curl --trace-ascii or use a single dash instead of file name to get it sent to stdout:

This shows you everything curl sends and receives, with some extra info thrown in.

This is the only answer that helped me. Some external script was setting some proxy settings and this told me the proxy was being used when it shouldn't have been. Verbose output did not mention the proxy.

I tried the answers here and found that the most useful and easiest one is not listed as an answer yet, but it is:

curl -v https://example.com/path 

This prints out the REQUEST headers as well as the RESPONSE headers plus other useful such as the SSL cert and whether an existing TCP connection was reused. the -v flag can be combined with other flags, of course, such as to follow redirects and prompt for HTTP authentication:

curl -vL --user my_username https://example.com/path 

A command like the one below will show three sections: request headers, response headers and data (separated by CRLF). It avoids technical information and syntactical noise added by curl.

curl -vs www.stackoverflow.com 2>&1 | sed '/^* /d; /bytes data]$/d; s/> //; s/< //' 

The command will produce the following output:

GET / HTTP/1.1 Host: www.stackoverflow.com User-Agent: curl/7.54.0 Accept: */* HTTP/1.1 301 Moved Permanently Content-Type: text/html; charset=UTF-8 Location: https://stackoverflow.com/ Content-Length: 149 Accept-Ranges: bytes Date: Wed, 16 Jan 2019 20:28:56 GMT Via: 1.1 varnish Connection: keep-alive X-Served-By: cache-bma1622-BMA X-Cache: MISS X-Cache-Hits: 0 X-Timer: S1547670537.588756,VS0,VE105 Vary: Fastly-SSL X-DNS-Prefetch-Control: off Set-Cookie: prov=e4b211f7-ae13-dad3-9720-167742a5dff8; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly  

Object Moved

This document may be found here
  • -vs - add headers (-v) but remove progress bar (-s)
  • 2>&1 - combine stdout and stderr into single stdout
  • sed - edit response produced by curl using the commands below
  • /^* /d - remove lines starting with '* ' (technical info)
  • /bytes data]$/d - remove lines ending with 'bytes data]' (technical info)
  • s/> // - remove '> ' prefix
  • s/ < // - remove '< ' prefix

Источник

Читайте также:  Sourcemod css v34 linux

cURL – How to display request headers and response headers

Request Header and Response Header are both a part of the HTTP protocol, which is the standard used for communication between web browsers and web servers. The Request Header is sent by the browser as part of an HTTP request, and it contains information such as the type of request, the URL of the requested page, and any authentication credentials. The Response Header is sent by the server in response to the request, and it contains information such as the status code of the response, the content type of the page, and any authentication credentials.

Together, the Request and Response Headers help to ensure that data is sent securely and accurately between the browser and the server. Request and Response Headers are essential for web developers as they provide important information for debugging and troubleshooting. If you’re interested in learning more about Request and Response Headers, a good place to start is by reading up on the HTTP protocol.

cURL is a command line utility used to transmit data over different-2 protocols. It is a quick tool for developers to view the request header and response header values of a website.

1. cURL – Get Request Headers

Use --versbose or -v option with the curl command to fetch the request header and response header values as following:

how to get request and response header with curl

2. cURL – Get Response Headers

You can also use curl to fetch the response header values only. Use -I option to get the response header values.

Output:
HTTP/1.1 301 Moved Permanently Location: http://www.google.com/ Content-Type: text/html; charset=UTF-8 Date: Sat, 10 Sep 2022 09:25:56 GMT Expires: Mon, 10 Oct 2022 09:25:56 GMT Cache-Control: public, max-age=2592000 Server: gws Content-Length: 219 X-XSS-Protection: 0 X-Frame-Options: SAMEORIGIN

3. cURL – Get Custom Header Values

Sometimes you may need to fetch the specific header value. That is helpful for scripting and many other tasks. Use the grep command to filter specific values from complete header values. The -F is used to search fixed string and -i is used for case-sensitive search.

curl -I google.com | grep -Fi "Content-Type" 
Output:
Content-Type: text/html; charset=UTF-8

Wrap Up

cURL is a command line utility that is helpful for multiple tasks. We can also use curl to request a server for the details. This tutorial helped you to get the request header and response header values using the curl command line.

Источник

Оцените статью
Adblock
detector