Contents

使用curl显示请求标头

1. 概述

curl 是一个有用的命令行工具,我们可以使用它通过计算机网络传输数据。在本教程中,我们将介绍几种显示curl发送到目标服务器的请求消息头的方法。

我们使用在 64 位 Debian 10.10 (Buster) 和 GNU bash 5.0.3 上运行的 64 位 curl 7.64.0 测试了代码。

2. 使用curl

使用curl 非常简单:

$ curl https://blogdemo.com
<!DOCTYPE html>
<!--[if lt IE 7]<html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7] <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8] <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--<html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
...

2.1.显示原始消息

我们将通过*-v* 或*–verbose*来显示我们发送到或从另一端接收的原始消息。它还将显示一堆 TCP/IP 协议消息,包括 SSL 握手过程:

$ curl -LvA "Mozilla" http://blogdemo.com
* Expire in 0 ms for 6 (transfer 0x55780f420fb0)
...
* Connected to blogdemo.com (104.26.12.74) port 80 (#0)
> GET / HTTP/1.1
> Host: blogdemo.com
> User-Agent: curl/7.64.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 05 Sep 2021 15:56:24 GMT
< Transfer-Encoding: chunked
< Connection: keep-alive
< Cache-Control: max-age=3600
< Expires: Sun, 05 Sep 2021 16:56:24 GMT
< Location: 
...
* Issue another request to this URL: ''
* Expire in 1 ms for 1 (transfer 0x55780f420fb0)
...
* Connected to blogdemo.com (104.26.12.74) port 443 (#1)
...
> GET / HTTP/2
> Host: blogdemo.com
> User-Agent: curl/7.64.0
> Accept: */*
> 
...
< HTTP/2 200 
< date: Sun, 05 Sep 2021 15:56:24 GMT
< content-type: text/html; charset=UTF-8
...

在上面的输出中,目标服务器以 301 响应第一条请求消息,这意味着它将请求的页面重定向到另一个位置。传递*-L* 或*–location* 参数将使curl自动跟随重定向。

-A 或*–user-agent* 参数表示用户代理。有些网络服务器需要它,有些则不需要。

2.2. 仅显示消息头

要仅显示消息头,我们将传递*-s* 或*–silent* 参数来抑制进度显示,并传递*-o /dev/null*来抑制接收到的正文:

$ curl -vsA "Mozilla" -o /dev/null https://www.blogdemo.com
* Expire in 0 ms for 6 (transfer 0x5589fa217fb0)
...
* Connected to blogdemo.com (104.26.12.74) port 443 (#0)
...
> GET / HTTP/2
> Host: blogdemo.com
> User-Agent: curl/7.64.0
> Accept: */*
>
...
< HTTP/2 200 
< date: Sun, 05 Sep 2021 16:23:50 GMT
< content-type: text/html; charset=UTF-8
...

在上面的输出中,它仍然显示 TCP/IP 和 SSL 内容。

** curl在请求消息之前放置一个“>”字符,在响应消息之前放置一个“<”**,由于 TCP/IP 和 SSL 日志消息通常没有这些字符,我们可以安全地获取包含它们的行。

让我们使用带有模式 ‘>|<’ 的grep 命令,这意味着我们想要从仅包含 ‘>’ 或 ‘<’ 字符的*curlcurl命令获取输出:

$ curl -vsA "Mozilla" -o /dev/null https://www.blogdemo.com 2>&1 | grep '>\|<'
> GET / HTTP/2
> Host: www.blogdemo.com
> User-Agent: Mozilla
> Accept: */*
> 
< HTTP/2 200 
< date: Sun, 05 Sep 2021 16:27:42 GMT
< content-type: text/html; charset=UTF-8
...

2 >&1意味着我们将stderr重定向到stdout,然后我们通过管道 / 将它们定向到grep命令。

我们还可以使用*sed *命令删除 TCP/IP 日志消息:

$ curl -vsA "Mozilla" -o /dev/null https://www.blogdemo.com 2>&1 | sed '/^[* {}]/d'
> GET / HTTP/2
> Host: www.blogdemo.com
> User-Agent: Mozilla
> Accept: */*
> 
< HTTP/2 200 
< date: Sun, 05 Sep 2021 16:30:03 GMT
< content-type: text/html; charset=UTF-8
...

描述:

  • /^[* {}]/d : 删除以 ‘*’、空格、’{’ 或 ‘}’ 字符开头的行

2.3. 仅显示请求消息头

要仅显示请求消息头,我们仍然可以使用grep来获取具有 ‘>’ 字符的行:

$ curl -vs https://www.blogdemo.com 2>&1 >/dev/null | grep '>'
> GET / HTTP/2
> Host: www.blogdemo.com
> User-Agent: curl/7.64.0
> Accept: */*
>

sed,通过删除以 ‘*’、空格、’{’、’}’ 或 ‘<’ 字符开头的行:

$ curl -vso /dev/null https://www.blogdemo.com 2>&1 | sed '/^[* {}<]/d'
> GET / HTTP/2
> Host: www.blogdemo.com
> User-Agent: curl/7.64.0
> Accept: */*
>

2.4. 仅显示清理请求消息头

让我们使用grepcut 命令清理curl的缩进和格式化样式的输出:

$ curl -vs https://www.blogdemo.com 2>&1 >/dev/null | grep '>' | cut -c1-2 --complement
GET / HTTP/2
Host: www.blogdemo.com
User-Agent: curl/7.64.0
Accept: */*

我们将curl命令的输出通过管道传输到grep命令,其中grep将过滤包含“>”字符的行。然后,我们将grep命令的输出通过管道传送到cut命令,其中cut将删除每行的前两个字符。

我们还可以使用sed清理输出:

$ curl -vso /dev/null https://www.blogdemo.com 2>&1 | sed '/^[* {}<]/d; s/> //;'
GET / HTTP/2
Host: www.blogdemo.com
User-Agent: curl/7.64.0
Accept: */*

我们将curl命令的输出通过管道传输到sed命令,其中sed将删除以“*”、空格、“{”、“}”和“<”字符开头的行,并且还将删除’> ’ 模式从每一行。