282
225

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

curlでヘッダを見る方法いろいろ

Posted at

--verbose

--verboseオプションをつけるとリクエストヘッダ、レスポンスヘッダ、httpsならTLS handshakeの様子等が出力されるようになります。なので、--verboseをつけるとヘッダが見れます。
また、この"verboseな"情報は標準エラー出力に出力されるので、ヘッダだけ見たい、と言う時は標準出力は/dev/nullに捨てちゃうといい感じに見やすくなります。

$ curl --verbose http://increments.co.jp/ 1> /dev/null
* About to connect() to increments.co.jp port 80 (#0)
*   Trying 75.101.145.87...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* connected
* Connected to increments.co.jp (75.101.145.87) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5
> Host: increments.co.jp
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx
< Date: Thu, 26 Dec 2013 18:00:31 GMT
< Content-Type: text/html;charset=utf-8
< Content-Length: 6624
< Connection: keep-alive
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
<
{ [data not shown]
100  6624  100  6624    0     0   6838      0 --:--:-- --:--:-- --:--:--  8406
* Connection #0 to host increments.co.jp left intact
* Closing connection #0

--head

httpメソッドにHEADというのがあって、これはbodyを返しません。で、このリクエストを送るオプションが--headです。レスポンスヘッダだけ見たい場合はこれが一番お手軽かもしれません。

$ curl --head http://increments.co.jp/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 26 Dec 2013 18:09:33 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 6624
Connection: keep-alive
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN

何が見たいかによりますが、ほしい情報がGETやPOSTとは結果が異なることがあるので注意ですね。

--include

--includeオプションをつけると、レスポンスヘッダも出力されます。

$ curl --include http://increments.co.jp/
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 26 Dec 2013 18:11:22 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 6624
Connection: keep-alive
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN

<!DOCTYPE html>
<html xmlns:og='http://ogp.me/ns#'>
  <head>
    <meta charset='UTF-8'>
    <title>Increments株式会社</title>
略

--dump-header

--dump-header FILENAME でFILENAMEファイルにレスポンスヘッダを出力します。

$ curl --dump-header header.log http://increments.co.jp/
<!DOCTYPE html>
<html xmlns:og='http://ogp.me/ns#'>
  <head>
    <meta charset='UTF-8'>
    <title>Increments株式会社</title>
略
$ cat header.log
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 26 Dec 2013 18:12:53 GMT
Content-Type: text/html;charset=utf-8
Content-Length: 6624
Connection: keep-alive
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN

FILENAMEのところでハイフン(-)を指定すると標準出力へ出力されます(=--includeと同じ)

--trace

--trace FILENAMEでtcpdumpのような通信内容がFILENAMEファイルに出力されます。ただ日本語には対応してないようです。

$ curl --trace trace.log http://increments.co.jp/ > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6624  100  6624    0     0   8023      0 --:--:-- --:--:-- --:--:-- 11420
$ head -50 trace.log
== Info: About to connect() to increments.co.jp port 80 (#0)
== Info:   Trying 174.129.212.2...
== Info: connected
== Info: Connected to increments.co.jp (174.129.212.2) port 80 (#0)
=> Send header, 147 bytes (0x93)
0000: 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1..
0010: 55 73 65 72 2d 41 67 65 6e 74 3a 20 63 75 72 6c User-Agent: curl
0020: 2f 37 2e 32 34 2e 30 20 28 78 38 36 5f 36 34 2d /7.24.0 (x86_64-
0030: 61 70 70 6c 65 2d 64 61 72 77 69 6e 31 32 2e 30 apple-darwin12.0
0040: 29 20 6c 69 62 63 75 72 6c 2f 37 2e 32 34 2e 30 ) libcurl/7.24.0
0050: 20 4f 70 65 6e 53 53 4c 2f 30 2e 39 2e 38 79 20  OpenSSL/0.9.8y
0060: 7a 6c 69 62 2f 31 2e 32 2e 35 0d 0a 48 6f 73 74 zlib/1.2.5..Host
0070: 3a 20 69 6e 63 72 65 6d 65 6e 74 73 2e 63 6f 2e : increments.co.
0080: 6a 70 0d 0a 41 63 63 65 70 74 3a 20 2a 2f 2a 0d jp..Accept: */*.
0090: 0a 0d 0a                                        ...
<= Recv header, 18 bytes (0x12)
0000: 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 20 HTTP/1.1 200 OK
0010: 0d 0a                                           ..
<= Recv header, 15 bytes (0xf)
0000: 53 65 72 76 65 72 3a 20 6e 67 69 6e 78 0d 0a    Server: nginx..
<= Recv header, 37 bytes (0x25)
0000: 44 61 74 65 3a 20 53 75 6e 2c 20 32 39 20 44 65 Date: Sun, 29 De
0010: 63 20 32 30 31 33 20 30 36 3a 31 36 3a 31 33 20 c 2013 06:16:13
0020: 47 4d 54 0d 0a                                  GMT..
<= Recv header, 39 bytes (0x27)
0000: 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 65 Content-Type: te
0010: 78 74 2f 68 74 6d 6c 3b 63 68 61 72 73 65 74 3d xt/html;charset=
0020: 75 74 66 2d 38 0d 0a                            utf-8..
<= Recv header, 22 bytes (0x16)
0000: 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 Content-Length:
0010: 36 36 32 34 0d 0a                               6624..
<= Recv header, 24 bytes (0x18)
0000: 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 70 Connection: keep
0010: 2d 61 6c 69 76 65 0d 0a                         -alive..
<= Recv header, 33 bytes (0x21)
0000: 58 2d 58 73 73 2d 50 72 6f 74 65 63 74 69 6f 6e X-Xss-Protection
0010: 3a 20 31 3b 20 6d 6f 64 65 3d 62 6c 6f 63 6b 0d : 1; mode=block.
0020: 0a                                              .
<= Recv header, 33 bytes (0x21)
0000: 58 2d 43 6f 6e 74 65 6e 74 2d 54 79 70 65 2d 4f X-Content-Type-O
0010: 70 74 69 6f 6e 73 3a 20 6e 6f 73 6e 69 66 66 0d ptions: nosniff.
0020: 0a                                              .
<= Recv header, 29 bytes (0x1d)
0000: 58 2d 46 72 61 6d 65 2d 4f 70 74 69 6f 6e 73 3a X-Frame-Options:
0010: 20 53 41 4d 45 4f 52 49 47 49 4e 0d 0a           SAMEORIGIN..
<= Recv header, 2 bytes (0x2)
0000: 0d 0a                                           ..
<= Recv data, 1196 bytes (0x4ac)
0000: 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 0a <!DOCTYPE html>.
0010: 3c 68 74 6d 6c 20 78 6d 6c 6e 73 3a 6f 67 3d 27 <html xmlns:og='

--trace-ascii

前項の--traceの16進数部分が邪魔、文字列化したやつだけ見せてくれ、って人は--trace-asciiオプションが良いです。同様に日本語は表示されません。

$ curl --trace-ascii trace.log http://increments.co.jp/ > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6624  100  6624    0     0   5611      0  0:00:01  0:00:01 --:--:-- 15622
$ head -35 trace.log
== Info: About to connect() to increments.co.jp port 80 (#0)
== Info:   Trying 75.101.163.44...
== Info: connected
== Info: Connected to increments.co.jp (75.101.163.44) port 80 (#0)
=> Send header, 147 bytes (0x93)
0000: GET / HTTP/1.1
0010: User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0
0050:  OpenSSL/0.9.8y zlib/1.2.5
006c: Host: increments.co.jp
0084: Accept: */*
0091:
<= Recv header, 18 bytes (0x12)
0000: HTTP/1.1 200 OK
<= Recv header, 15 bytes (0xf)
0000: Server: nginx
<= Recv header, 37 bytes (0x25)
0000: Date: Sun, 29 Dec 2013 06:20:51 GMT
<= Recv header, 39 bytes (0x27)
0000: Content-Type: text/html;charset=utf-8
<= Recv header, 22 bytes (0x16)
0000: Content-Length: 6624
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 33 bytes (0x21)
0000: X-Xss-Protection: 1; mode=block
<= Recv header, 33 bytes (0x21)
0000: X-Content-Type-Options: nosniff
<= Recv header, 29 bytes (0x1d)
0000: X-Frame-Options: SAMEORIGIN
<= Recv header, 2 bytes (0x2)
0000:
<= Recv data, 2644 bytes (0xa54)
0000: <!DOCTYPE html>.<html xmlns:og='http://ogp.me/ns#'>.  <head>.
0040:  <meta charset='UTF-8'>.    <title>Increments............</title
0080: >.    <meta content='Increments.................................
282
225
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
282
225

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?