コマンドラインcurlでリクエストヘッダを表示する方法

コマンドラインのcurlは、-Dオプションを使ってレスポンスヘッダを表示することができますが、どのようなリクエストヘッダを送信しているかを確認したいのです。どうすればいいですか?

質問へのコメント (3)

curl'の -v または --verbose オプションは、HTTPリクエストヘッダを表示します。以下に出力例を示す:

$ curl -v http://google.com/
* About to connect() to google.com port 80 (#0)
*   Trying 66.102.7.104... connected
* Connected to google.com (66.102.7.104) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3
> Host: google.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.com/
< Content-Type: text/html; charset=UTF-8
< Date: Thu, 15 Jul 2010 06:06:52 GMT
< Expires: Sat, 14 Aug 2010 06:06:52 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 219
< X-XSS-Protection: 1; mode=block
< 
<meta http-equiv="content-type" content="text/html;charset=utf-8">
301 Moved
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.

* Connection #0 to host google.com left intact
* Closing connection #0
解説 (2)

response ヘッダーを表示するための一般的な回答ですが、OPは request ヘッダーについて尋ねました。*。

curl -sD - -o /dev/null http://example.com

--s-進行状況バーを表示しないでください。 --D --ヘッダーをファイルにダンプしますが、 -はそれをstdoutに送信します。 --o / dev / null-応答本文を無視します。

これは、異なる結果を生成する可能性がある「HEAD」リクエストを送信しないため、「-I」よりも優れています。

反響を解くためにそれほど多くのハックを必要としないので、それは「-v」よりも優れています。

解説 (7)

curlに渡すコマンドラインスイッチは-Iだと思います。

使用例:

$ curl -I http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287  
HTTP/1.1 301 Moved Permanently
Date: Sat, 29 Dec 2012 15:22:05 GMT
Server: Apache
Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/
Content-Type: text/html; charset=iso-8859-1

さらに、レスポンスの HTTP ステータス コードが 301 の場合、-L 引数スイッチを渡すと、URL リダイレクトに従うように curl に指示することができます:

$ curl -I -L http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287
HTTP/1.1 301 Moved Permanently
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Location: http://heatmiser.counterhack.com/zone-5-15614E3A-CEA7-4A28-A85A-D688CC418287/
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 302 Found
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Set-Cookie: UID=b8c37e33defde51cf91e1e03e51657da
Location: noaccess.php
Content-Type: text/html

HTTP/1.1 200 OK
Date: Sat, 29 Dec 2012 15:22:13 GMT
Server: Apache
Content-Type: text/html
解説 (4)

verboseオプションは便利だが、curlが行うこと(ヘッダーだけでなく、送信されるHTTPボディも含む)をすべて見たい場合は、以下のオプションのいずれかを使うことをお勧めする:

  • --trace-ascii - # stdout
  • --trace-ascii output_file.txt # ファイル
解説 (1)

次のコマンドで素敵なヘッダー出力を取得します。

 curl -L -v -s -o /dev/null google.de

--L、-locationリダイレクトに従います。 --v、-verboseより多くの出力は方向を示します。 --s、--silentは進行状況バーを表示しません。 --o、-output / dev / nullは受信したボディを表示しません。

または短いバージョン:

 curl -Lvso /dev/null google.de

結果:

* Rebuilt URL to: google.de/
*   Trying 2a00:1450:4008:802::2003...
* Connected to google.de (2a00:1450:4008:802::2003) port 80 (#0)
> GET / HTTP/1.1
> Host: google.de
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: http://www.google.de/
< Content-Type: text/html; charset=UTF-8
< Date: Fri, 12 Aug 2016 15:45:36 GMT
< Expires: Sun, 11 Sep 2016 15:45:36 GMT
< Cache-Control: public, max-age=2592000
< Server: gws
< Content-Length: 218
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
<
* Ignoring the response-body
{ [218 bytes data]
* Connection #0 to host google.de left intact
* Issue another request to this URL: 'http://www.google.de/'
*   Trying 2a00:1450:4008:800::2003...
* Connected to www.google.de (2a00:1450:4008:800::2003) port 80 (#1)
> GET / HTTP/1.1
> Host: www.google.de
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 12 Aug 2016 15:45:36 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: NID=84=Z0WT_INFoDbf_0FIe_uHqzL9mf3DMSQs0mHyTEDAQOGY2sOrQaKVgN2domEw8frXvo4I3x3QVLqCH340HME3t1-6gNu8R-ArecuaneSURXNxSXYMhW2kBIE8Duty-_w7; expires=Sat, 11-Feb-2017 15:45:36 GMT; path=/; domain=.google.de; HttpOnly
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
{ [11080 bytes data]
* Connection #1 to host www.google.de left intact

ご覧のとおり、「カール」は発信ヘッダーと着信ヘッダーの両方を出力し、ボディデータをスキップして、ボディの大きさを伝えます。

さらに、すべての線について方向が示されているため、読みやすくなっています。 リダイレクトの長いチェーンを追跡することは特に有用であることがわかりました。

解説 (1)

以下のようなコマンドには、リクエストヘッダー、応答ヘッダー、およびデータ(CRLFで区切る)の3つのセクションが表示されます。 カールによって追加される技術情報と構文ノイズを回避します。

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

Document Moved
<h1>Object Moved</h1>This document may be found <a HREF="https://stackoverflow.com/">here</a>

説明:

--vs-ヘッダー(-v)を追加しますが、進行状況バー(-s)を削除します。 -2>& 1-stdoutとstderrを1つのstdoutに結合します。 -sed-以下のコマンドを使用してcurlによって作成された応答を編集します。 -/ ^ * / d-'* 'で始まる行を削除します(技術情報)。 -/バイトデータ] $ / d-「バイトデータ]」で終わる行を削除します(技術情報)。 -s /> //-'>を削除します。 '接頭辞。 -s /< //-'

解説 (0)

Webアプリケーションをデバッグするとき、私はこの問題を自分で克服しなければなりませんでした。 -vは素晴らしいですが、私の好みには少し冗長です。 これは私が思いついた(バッシュのみ)ソリューションです。

curl -v http://example.com/ 2> >(sed '/^*/d')

これは、 -vからの出力がstdoutではなくstderrに送信されるために機能します。 これをサブシェルにリダイレクトすることで、「*」で始まる行を削除するように「送信」できます。 実際の出力はサブシェルを通過しないため、影響を受けません。 サブシェルを使用するのは少し強引ですが、stderrを別のコマンドにリダイレクトする最も簡単な方法です。 (前述のとおり、これはテストにのみ使用しているため、問題なく動作します。)。

解説 (0)

curlの-vオプションは、先頭の *(ステータス行)または >(リクエストヘッドフィールド)または<(レスポンスヘッドフィールド)を含むエラー出力で冗長すぎます。 リクエストヘッドフィールドのみを取得するには:

curl -v -sS www.stackoverflow.com 2>&1 >/dev/null | grep '>' | cut -c1-2 --complement

リクエストヘッドフィールドのみを取得するには:


curl -v -sS www.stackoverflow.com 2>&1 >/dev/null | grep '
解説 (0)

代替案が必要な場合は、httpieのようなModernコマンドラインHTTPクライアントをインストールしてみてください。これは、brew、apt-get、pip、yumなどのパッケージマネージャーがいるほとんどのオペレーティングシステムで利用できます。

例:-OSXの場合。

brew install httpie

次に、さまざまなオプションを使用してコマンドラインでそれを使用できます。

http GET https://www.google.com

。![ここに画像の説明を入力してください](https://i.stack.imgur.com/6RIUO.png。)

解説 (0)