LoginSignup
5
3

More than 5 years have passed since last update.

POSTで送信されたデータを確認する方法

Posted at

Web開発をおこなっているとPOSTされたデータを確認したい場合があります。
今回はPHPのビルトインサーバーに対してPOSTしたデータを2種類の方法で確認してみます。

なお、環境はOS X El Capitanとなります。

ビルトインサーバーを起動しておく

$ mkdir ~/webroot
$ cd ~/webroot
$ vim index.html
index.html
Hello World.
$ cd
$ php -S localhost:8080 -t ~/webroot

PHPのビルトインサーバーについては詳しくは公式マニュアルを
http://php.net/manual/ja/features.commandline.webserver.php

tcpdump

tcpdumpはTCP/IPなどのパケットをダンプして表示させることができるツールです。

実行例

$ sudo tcpdump -A -p -s 0 -i lo0 -n 'dst port 8080 and greater 100'

オプション解説

-A

パケットの内容をASCIIで表示する。

-p

非プロミスキャスモードで動作させる。自ホスト宛のデータのみキャプチャする。

-s size

パケットから取得するサイズを指定する。0を指定した場合には無制限に取得する。

-i interface

インターフェースを指定する。今回はビルトインサーバーから取得するのでlo0を指定する。

-n

ホスト名やポート番号を変換せずにそのまま表示。

'dst port 8080 and greater 100'

ダンプするパケットのフィルター

dst port 8080
パケットの宛先ポート番号を指定

greater length
パケットがlengthで指定した長さ以上の場合に真

tcpdumpを試す

curlでデータをPOSTする

$ curl -sS --include -X POST 'http://localhost:8080' -d 'a=1'

HTTP/1.1 200 OK
Host: localhost:8080
Connection: close
Content-Type: text/html; charset=UTF-8
Content-Length: 13

Hello World.

tcpdumpでパケットをキャプチャする

$ sudo tcpdump -A -p -s0 -i lo0 -n 'dst port 8080 and greater 100'

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo0, link-type NULL (BSD loopback), capture size 262144 bytes
07:11:54.078079 IP6 ::1.52963 > ::1.8080: Flags [P.], seq 1880446489:1880446639, ack 499426002, win 12743, options [nop,nop,TS val 785863894 ecr 785863894], length 150: HTTP: POST / HTTP/1.1
`.{....@....................................p.V.......1........
..T...T.POST / HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.43.0
Accept: */*
Content-Length: 3
Content-Type: application/x-www-form-urlencoded

a=1

a=1とPOSTしたデータが確認することができます。

ngrep

grepの様に使える、パケットをキャプチャできるツール。
http://ngrep.sourceforge.net/

インストール

$ brew install ngrep

実行例

$ sudo ngrep -d lo0 -W byline port 8080

オプション解説

-d

LISTENするインターフェースの指定

-W

表示方法、bylineを指定するとlinefeedsで改行する。

port

パケットの送信元ポートまたは宛先ポートを指定する。

ngrepを試す

curlでデータをPOSTする

$ curl -sS --include -X POST 'http://localhost:8080' -d 'a=1' -d 'b=2' -d 'c=3'

ngrepでパケットをキャプチャする

$ sudo ngrep -d lo0 -W byline port 8080

interface: lo0 (127.0.0.0/255.0.0.0)
filter: (ip or ip6) and ( port 8080 )
#####
T ::1:50538 -> ::1:8080 [AP]
POST / HTTP/1.1.
Host: localhost:8080.
User-Agent: curl/7.43.0.
Accept: */*.
Content-Length: 11.
Content-Type: application/x-www-form-urlencoded.
.
a=1&b=2&c=3

a=1&b=2&c=3とPOSTしたデータが確認することができます。

最後に

より便利なオプションや使い方があれば教えて頂けたら幸いです。

ここまで読んでいただき、ありがとうございました。

5
3
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
5
3