I want to see the request headers made by curl
when I am sending a request to the server. How can I check that?
views:
2331answers:
7curl -D file url
curl --dump-headers file url
See:
curl --help | less
The --trace
or --trace-ascii
options are correct for the headers.
For example, the request:
curl --trace-ascii curl.trace --url http://www.google.com/
produced the initial output:
== 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 (an 302 response, to be precise but irrelevant) which was logged.
Charles is a nice http debugging proxy, available for windows, bsd and linux.
curl --trace-ascii dump.txt ...
CURLOPT_DEBUGFUNCTION if you're using libcurl
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);
Getting your debug info:
$data = curl_exec($ch);
var_dump $data;
var_dump curl_getinfo($ch);
I think curl -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
...
Here is my http client in php to make post queries with cookies included:
function http_login_client($url, $params = "", $cookies_send = "" ){
// Vars
$cookies = array();
$headers = getallheaders();
// Perform a http post request to $ur1 using $params
$ch = curl_init($url);
$options = array( CURLOPT_POST => 1,
CURLINFO_HEADER_OUT => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_COOKIE => $cookies_send,
CURLOPT_USERAGENT => $headers['User-Agent']
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
/// DEBUG info echo $response; var_dump (curl_getinfo($ch)); ///
// Parse response and read cookies
preg_match_all('/^Set-Cookie: (.*?)=(.*?);/m', $response, $matches);
// Build an array with cookies
foreach( $matches[1] as $index => $cookie )
$cookies[$cookie] = $matches[2][$index];
return $cookies;
} // end http_login_client