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


当前回答

向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了解更多细节。

其他回答

唯一的方法,我设法看到我的外向头(卷曲与php)是使用以下选项:

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));

我认为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
...

如下所示的命令将显示三个部分:请求头、响应头和数据(由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 /< // -删除'< '前缀

curl -s -v -o/dev/null -H "Testheader: test" http://www.example.com

如果你想发送HEAD请求而不是GET请求,你也可以使用-I选项。

你可以通过使用"-"作为文件名直接将头文件转储到标准输出,例如查看头文件和内容,并遵循重定向,所有这些都在一个输出中:

curl -L -D - -s [url]