I want to see the request headers made by curl when I am sending a request to the server. How can I check that?
I want to see the request headers made by curl when I am sending a request to the server. How can I check that?
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.
<?php
$ch = curl_init();
$f = tmpfile(); # will be automatically removed after fclose()
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => $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;
Example usage:
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
The --trace-ascii option to curl will show the request headers, as well as the response headers and response body.
For example, the command
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.
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));
curl --trace-ascii {filename} or use a single dash instead of file name to get it sent to stdout:
curl --trace-ascii - {URL}
CURLOPT_DEBUGFUNCTION if you're using libcurl
This shows you everything curl sends and receives, with some extra info thrown in.
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
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="https://stackoverflow.com/">here</a></body>
Description:
-vs - add headers (-v) but remove progress bar (-s)2>&1 - combine stdout and stderr into single stdoutsed - 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 '> ' prefixs/< // - remove '< ' prefixYou can dump headers directly to stdout by using "-" as the filename - so for example to see headers and content, and follow redirects, all in one output:
curl -L -D - -s [url]