当我向服务器发送请求时,我想看到curl所做的请求头。我要怎么检查呢?


当前回答

我尝试了这里的答案,发现最有用和最简单的一个还没有被列为答案,但它是:

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

这将打印出REQUEST报头、RESPONSE报头以及其他有用的信息,如SSL证书以及是否重用了现有的TCP连接。当然,-v标志可以与其他标志结合使用,例如跟随重定向和HTTP认证提示:

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

希望这能有所帮助。

其他回答

下面是我的http客户端在php使post查询包含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);

///调试信息 echo $响应; 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

如下所示的命令将显示三个部分:请求头、响应头和数据(由CRLF分隔)。它避免了curl添加的技术信息和语法噪声。

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

该命令将产生如下输出:

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>

描述:

-vs -添加报头(-v)但删除进度条(-s) 2>&1 -将stdout和stderr合并为单个stdout 使用下面的命令由curl生成的Sed编辑响应 /^* /d -删除以“*”开头的行(技术信息) $/d -删除以'bytes data]结尾的行(技术信息) S /> // -删除'> '前缀 S /< // -删除'< '前缀

向https://http-tools.appspot.com/reflect-http-request/some-unique-id发送一个示例请求,并通过其对应的查找器url https://http-tools.appspot.com/reflect-http-request-finder/some-unique-id检查该请求包含什么(请求头、请求体、请求参数)。你可以使用任何字符串而不是某个唯一的id,查看https://http-tools.appspot.com了解更多细节。

我认为curl -verbose/-v是最简单的。它将输出请求头(以'>'为前缀的行),而无需写入文件:

$ 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 -k -v -u user:pass  "url" --trace-ascii headers.txt >> response.txt