使用curl

时间:2017-11-02 17:03:19

标签: curl

curl命令:

curl -IL "http://www.bbc.co.uk"

返回:

HTTP/1.1 301 Moved Permanently
Server: nginx
X-BBC-No-Scheme-Rewrite: 1
X-Cache-Action: HIT
X-Cache-Hits: 18686
Vary: X-BBC-Edge-Scheme
Cache-Control: public, max-age=3600
X-Cache-Age: 2624
Content-Type: text/html
Date: Thu, 02 Nov 2017 16:55:53 GMT
Location: https://www.bbc.co.uk/
Content-Length: 178
Connection: Keep-Alive

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
ETag: W/"3fdf4-XNfGeb/Aqcmv8OYZKVQdoUFYC34"
X-Frame-Options: SAMEORIGIN
Content-Length: 261620
Date: Thu, 02 Nov 2017 16:55:53 GMT
Connection: keep-alive
Set-Cookie: BBC-UID=65699f7bb41e91e9d7e103d8618f7780367bf9e637843476ca70678244f407000curl/7.19.7%20(x86_64-redhat-linux-gnu)%20libcurl/7.19.7%20NSS/3.19.1%20Basic%20ECC%20zlib/1.2.3%20libidn/1.18%20libssh2/1.4.2; expires=Mon, 01-Nov-21 16:55:53 GMT; path=/; domain=.bbc.co.uk
X-Cache-Action: HIT
X-Cache-Hits: 5
X-Cache-Age: 0
Cache-Control: private, max-age=0, must-revalidate
Vary: Accept-Encoding, X-CDN, X-BBC-Edge-Scheme

(在制作中,我使用-i而非-I来获取整个有效负载)

如何让curl隐藏发出重定向的前面标题,然后返回最终目标资源的标题?

1 个答案:

答案 0 :(得分:1)

curl中没有这样的选项,但只需要这个简单的sed程序:

sed '/^HTTP\/1.1 3[0-9][0-9]/,/^\r$/d' file

如果您通过它输出curl输出,它将删除(注意最后的d action)所有以任何3xx重定向代码(HTTP/1.1 3[0-9][0-9]开头的标头块并且以空行结束(Unix中的空行实际上只是\n,在sed中以^$模式识别,但HTTP标头使用DOS换行序列\r\nsed剥离后变为\r;因此我们必须匹配^\r$)。要选择一系列行,sed range addressingregexp addresses一起使用。

对于你的例子:

$ curl -sIL "http://www.bbc.co.uk" | sed '/^HTTP\/1.1 3[0-9][0-9]/,/^\r$/d'
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
ETag: W/"3fbed-SZN5fj1ugmv3uhAJWfQBrAvzK1M"
X-Frame-Options: SAMEORIGIN
Content-Length: 261101
Date: Thu, 02 Nov 2017 22:53:19 GMT
Connection: keep-alive
Set-Cookie: BBC-UID=95994f2b2a4bf...57f26d5442240curl/7.50.1; expires=Mon, 01-Nov-21 22:53:19 GMT; path=/; domain=.bbc.co.uk
X-Cache-Action: HIT
X-Cache-Hits: 649
X-Cache-Age: 119
Cache-Control: private, max-age=0, must-revalidate
Vary: Accept-Encoding, X-CDN, X-BBC-Edge-Scheme

完全回应(与身体)相似:

$ curl -siL "http://www.bbc.co.uk" | sed '/^HTTP\/1.1 3[0-9][0-9]/,/^\r$/d'

注意我们上面使用的-s标记来抑制curl的进度输出。