I want to see the request headers made by curl
when I am sending a request to the server. How can I check that?
我希望在向服务器发送请求时看到curl发出的请求标头。我怎么检查?
13 个解决方案
#1
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是最简单的。它会吐出请求标题(前缀为'>'的行),而不必写入文件:
$ curl -v -I -H "Testing: Test header so you see this works" http://*.com/
* About to connect() to *.com port 80 (#0)
* Trying 69.59.196.211... connected
* Connected to *.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: *.com
> Accept: */*
> Testing: Test header so you see this works
>
< HTTP/1.0 200 OK
...
#2
The question did not specify if command line command named curl
was meant or the whole cURL library.
该问题没有指定是否意味着名为curl的命令行命令或整个cURL库。
The following PHP code using cURL library uses first parameter as HTTP method (e.g. "GET", "POST", "OPTIONS") and second parameter as URL.
以下使用cURL库的PHP代码使用第一个参数作为HTTP方法(例如“GET”,“POST”,“OPTIONS”),第二个参数作为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
#3
The only way I managed to see my outgoing headers (curl with php) was using the following options:
我设法看到我的传出标题(用curl卷曲)的唯一方法是使用以下选项:
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));
#4
The --trace-ascii
option to curl will show the request headers, as well as the response headers and response body.
curl的--trace-ascii选项将显示请求标头,以及响应标头和响应正文。
For example, the command
例如,命令
curl --trace-ascii curl.trace http://www.google.com/
produces a file curl.trace
that starts as follows:
生成一个文件curl.trace,开头如下:
== 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.
它还得到了一个响应(一个302响应,确切但无关紧要)。
If you only want to save the response headers, use the --dump-header
option:
如果您只想保存响应标头,请使用--dump-header选项:
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.
如果您需要有关可用选项的更多信息,请使用curl --help |更少(它产生几百行输出,但提到了很多选项)。或者找到手册页,其中有更多解释选项的含义。
#5
curl --trace-ascii {filename} or use a single dash instead of file name to get it sent to stdout:
curl --trace-ascii {filename}或使用单个破折号而不是文件名将其发送到stdout:
curl --trace-ascii - {URL}
CURLOPT_DEBUGFUNCTION if you're using libcurl
CURLOPT_DEBUGFUNCTION如果您正在使用libcurl
This shows you everything curl sends and receives, with some extra info thrown in.
这会向您显示curl发送和接收的所有内容,并提供一些额外的信息。
#6
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:
这将打印出REQUEST标头以及RESPONSE标头以及其他有用的信息,例如SSL证书以及是否重用现有的TCP连接。当然,-v标志可以与其他标志组合,例如遵循重定向并提示进行HTTP身份验证:
curl -vL --user my_username https://example.com/path
Hope this helps.
希望这可以帮助。
#7
I know this is a little late, but my favoured method for doing this is netcat
, as you get exactly what curl
sent; this can differ from the --trace
or --trace-ascii
options which won't show non-ASCII characters properly (they just show as dots or need to be decoded).
我知道这有点晚了,但我最喜欢这样做的方法是netcat,因为你得到了curl发送的内容;这可能与--trace或--trace-ascii选项不同,后者不会正确显示非ASCII字符(它们只显示为点或需要解码)。
You can do this as very easily by opening two terminal windows, in the first type:
您可以通过打开第一个类型的两个终端窗口来轻松完成此操作:
nc -l localhost 12345
This opens a listening process on port 12345 of your local machine.
这将在本地计算机的端口12345上打开侦听进程。
In the second terminal window enter your curl command, for example:
在第二个终端窗口中输入curl命令,例如:
curl --form 'foo=bar' localhost:12345
In the first terminal window you will see exactly what curl sent in the request.
在第一个终端窗口中,您将看到请求中发送的卷曲。
Now of course nc
won't send anything in response (unless you type it in yourself), so you will need to interrupt the curl command (control-c) and repeat the process for each test.
当然,当然nc不会发送任何响应(除非你自己输入),所以你需要中断curl命令(control-c)并重复每个测试的过程。
However, this is a useful option for simply debugging your request, as you're not involving a round-trip anywhere, or producing bogus, iterative requests somewhere until you get it right; once you're happy with the command, simply redirect it to a valid URL and you're good to go.
但是,这对于简单地调试您的请求是一个有用的选项,因为您不会在任何地方进行往返,或者在某个地方产生虚假的迭代请求,直到您做到正确为止;一旦你对命令感到满意,只需将其重定向到一个有效的URL,你就可以了。
You can do the same for any cURL library as well, simply edit your request to point to the local nc
listener until you're happy with it.
您也可以对任何cURL库执行相同操作,只需编辑您的请求以指向本地nc侦听器,直到您对它感到满意为止。
#8
curl -s -v -o/dev/null -H "Testheader: test" http://www.example.com
You could also use -I
option if you want to send a HEAD request and not a GET request.
如果要发送HEAD请求而不是GET请求,也可以使用-I选项。
#9
dump the headers in one file and the payload of the response in a different file
将标头转储到一个文件中,将响应的有效负载转储到另一个文件中
curl -k -v -u user:pass "url" --trace-ascii headers.txt >> response.txt
#10
Here is my http client in php to make post queries with cookies included:
这是我在php中的http客户端,用于包含cookie的帖子查询:
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)); ///
/// 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
#11
You can see it by using -iv
您可以使用-iv查看它
$> curl -ivH "apikey:ad9ff3d36888957" --form "file=@/home/mar/workspace/images/8.jpg" --form "language=eng" --form "isOverlayRequired=true" https://api.ocr.space/Parse/Image
#12
#13
Make a sample request to https://http-tools.appspot.com/reflect-http-request/some-unique-id and check what this request contains (request header, request body, request parameters) by its corresponding finder url https://http-tools.appspot.com/reflect-http-request-finder/some-unique-id. You can use any string instead of some-unique-id
, check out https://http-tools.appspot.com for more details.
向https://http-tools.appspot.com/reflect-http-request/some-unique-id发出示例请求,并通过相应的查找器URL https检查此请求包含的内容(请求标头,请求正文,请求参数) ://http-tools.appspot.com/reflect-http-request-finder/some-unique-id。您可以使用任何字符串而不是some-unique-id,请查看https://http-tools.appspot.com以获取更多详细信息。
#1
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是最简单的。它会吐出请求标题(前缀为'>'的行),而不必写入文件:
$ curl -v -I -H "Testing: Test header so you see this works" http://*.com/
* About to connect() to *.com port 80 (#0)
* Trying 69.59.196.211... connected
* Connected to *.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: *.com
> Accept: */*
> Testing: Test header so you see this works
>
< HTTP/1.0 200 OK
...
#2
The question did not specify if command line command named curl
was meant or the whole cURL library.
该问题没有指定是否意味着名为curl的命令行命令或整个cURL库。
The following PHP code using cURL library uses first parameter as HTTP method (e.g. "GET", "POST", "OPTIONS") and second parameter as URL.
以下使用cURL库的PHP代码使用第一个参数作为HTTP方法(例如“GET”,“POST”,“OPTIONS”),第二个参数作为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
#3
The only way I managed to see my outgoing headers (curl with php) was using the following options:
我设法看到我的传出标题(用curl卷曲)的唯一方法是使用以下选项:
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));
#4
The --trace-ascii
option to curl will show the request headers, as well as the response headers and response body.
curl的--trace-ascii选项将显示请求标头,以及响应标头和响应正文。
For example, the command
例如,命令
curl --trace-ascii curl.trace http://www.google.com/
produces a file curl.trace
that starts as follows:
生成一个文件curl.trace,开头如下:
== 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.
它还得到了一个响应(一个302响应,确切但无关紧要)。
If you only want to save the response headers, use the --dump-header
option:
如果您只想保存响应标头,请使用--dump-header选项:
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.
如果您需要有关可用选项的更多信息,请使用curl --help |更少(它产生几百行输出,但提到了很多选项)。或者找到手册页,其中有更多解释选项的含义。
#5
curl --trace-ascii {filename} or use a single dash instead of file name to get it sent to stdout:
curl --trace-ascii {filename}或使用单个破折号而不是文件名将其发送到stdout:
curl --trace-ascii - {URL}
CURLOPT_DEBUGFUNCTION if you're using libcurl
CURLOPT_DEBUGFUNCTION如果您正在使用libcurl
This shows you everything curl sends and receives, with some extra info thrown in.
这会向您显示curl发送和接收的所有内容,并提供一些额外的信息。
#6
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:
这将打印出REQUEST标头以及RESPONSE标头以及其他有用的信息,例如SSL证书以及是否重用现有的TCP连接。当然,-v标志可以与其他标志组合,例如遵循重定向并提示进行HTTP身份验证:
curl -vL --user my_username https://example.com/path
Hope this helps.
希望这可以帮助。
#7
I know this is a little late, but my favoured method for doing this is netcat
, as you get exactly what curl
sent; this can differ from the --trace
or --trace-ascii
options which won't show non-ASCII characters properly (they just show as dots or need to be decoded).
我知道这有点晚了,但我最喜欢这样做的方法是netcat,因为你得到了curl发送的内容;这可能与--trace或--trace-ascii选项不同,后者不会正确显示非ASCII字符(它们只显示为点或需要解码)。
You can do this as very easily by opening two terminal windows, in the first type:
您可以通过打开第一个类型的两个终端窗口来轻松完成此操作:
nc -l localhost 12345
This opens a listening process on port 12345 of your local machine.
这将在本地计算机的端口12345上打开侦听进程。
In the second terminal window enter your curl command, for example:
在第二个终端窗口中输入curl命令,例如:
curl --form 'foo=bar' localhost:12345
In the first terminal window you will see exactly what curl sent in the request.
在第一个终端窗口中,您将看到请求中发送的卷曲。
Now of course nc
won't send anything in response (unless you type it in yourself), so you will need to interrupt the curl command (control-c) and repeat the process for each test.
当然,当然nc不会发送任何响应(除非你自己输入),所以你需要中断curl命令(control-c)并重复每个测试的过程。
However, this is a useful option for simply debugging your request, as you're not involving a round-trip anywhere, or producing bogus, iterative requests somewhere until you get it right; once you're happy with the command, simply redirect it to a valid URL and you're good to go.
但是,这对于简单地调试您的请求是一个有用的选项,因为您不会在任何地方进行往返,或者在某个地方产生虚假的迭代请求,直到您做到正确为止;一旦你对命令感到满意,只需将其重定向到一个有效的URL,你就可以了。
You can do the same for any cURL library as well, simply edit your request to point to the local nc
listener until you're happy with it.
您也可以对任何cURL库执行相同操作,只需编辑您的请求以指向本地nc侦听器,直到您对它感到满意为止。
#8
curl -s -v -o/dev/null -H "Testheader: test" http://www.example.com
You could also use -I
option if you want to send a HEAD request and not a GET request.
如果要发送HEAD请求而不是GET请求,也可以使用-I选项。
#9
dump the headers in one file and the payload of the response in a different file
将标头转储到一个文件中,将响应的有效负载转储到另一个文件中
curl -k -v -u user:pass "url" --trace-ascii headers.txt >> response.txt
#10
Here is my http client in php to make post queries with cookies included:
这是我在php中的http客户端,用于包含cookie的帖子查询:
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)); ///
/// 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
#11
You can see it by using -iv
您可以使用-iv查看它
$> curl -ivH "apikey:ad9ff3d36888957" --form "file=@/home/mar/workspace/images/8.jpg" --form "language=eng" --form "isOverlayRequired=true" https://api.ocr.space/Parse/Image
#12
You can use wireshark or tcpdump to look on any network traffic (http too).
您可以使用wireshark或tcpdump查看任何网络流量(http也是如此)。
#13
Make a sample request to https://http-tools.appspot.com/reflect-http-request/some-unique-id and check what this request contains (request header, request body, request parameters) by its corresponding finder url https://http-tools.appspot.com/reflect-http-request-finder/some-unique-id. You can use any string instead of some-unique-id
, check out https://http-tools.appspot.com for more details.
向https://http-tools.appspot.com/reflect-http-request/some-unique-id发出示例请求,并通过相应的查找器URL https检查此请求包含的内容(请求标头,请求正文,请求参数) ://http-tools.appspot.com/reflect-http-request-finder/some-unique-id。您可以使用任何字符串而不是some-unique-id,请查看https://http-tools.appspot.com以获取更多详细信息。