LoginSignup
5
1

More than 3 years have passed since last update.

HTTP POST でデータを送って中身を確認したい

Last updated at Posted at 2019-09-04

はじめに

POSTで送るデータの中身を簡単に見たい場合、
AWSのAPIGatewayに投げるのが楽ですが、'HTTPS'でしかPOST出来ません。
訳あって'HTTP'でPOSTしたい場合どうしよう…と思い、Apacheのログで見ることにしました。

EC2上にApacheをインストール&設定

まずはインストール

# yum -y install httpd
..(インストール完了)..
# httpd -version
Server version: Apache/2.4.39 ()
Server built:   Apr  4 2019 18:09:28

自動起動の設定

インスタンスを立ち上げた時にApacheも同時に起動するように設定。

# systemctl enable httpd.service

起動して、無事にアクセスできることを確認。

# systemctl start httpd.service

EC2インスタンスに割り当てられたIPアドレス叩いてApacheのテストページが出たらOK:ok_woman:

POSTログを残すための設定

dumpioモジュールが入っていることを確認

# httpd -M | grep dumpio
dumpio_module (shared)  ←やったー、入ってる!

configファイルにモジュールを使用する旨&ログ出力の部分の記載をする

# vi /etc/httpd/conf/httpd.conf

下記を追記。(レスポンスの出力は不要なのでDumpIOOutput は記載しません)

httpd.conf
LoadModule dumpio_module      modules/mod_dumpio.so
DumpIOInput On

ログレベルを変更する(元はLogLevel warnとなっていました)

LogLevel debug dumpio:trace7

変更を終えたらApacheを再起動。

# systemctl restart httpd.service

フォームを持ったindex.htmlを用意

/var/www/html内にindex.htmlを作る

index.html
<html>
<head><meta charset="utf-8"></head>
<body>
<form method="POST">
        <input type="text" name="id">
        <input type="submit" name="submit" value="send message">
</form>
</body>
</html>

POSTしてみる!

$ curl -X POST -F "id=testID" http://xxxxxxx  ←EC2インスタンスのIP

ログ調査

# tail -n 20 /var/log/httpd/error_log

ありましたー。

[Wed Sep 04 00:49:42.398487 2019] [dumpio:trace7] [pid 3497] mod_dumpio.c(103): [client xx.xx.xx.xx:xxxxx] mod_dumpio:  dumpio_in (data-HEAP): --------------------------1ebdfe62e1c40235\r\nContent-Disposition: form-data; name="id"\r\n\r\ntestID\r\n--------------------------1ebdfe62e1c40235--\r\n

見づらいので、ログを整形して、欲しい部分だけ抜き出します。

# grep 'name="id"\\r\\n\\r\\n' /var/log/httpd/error_log | sed -e 's/^.*name="id"\\r\\n\\r\\n\([0-9a-zA-Z_]*\).*$/\1/'
testID

とれたー:raised_hands:
とりあえず確認用だったら割と簡単に出来るので良いなぁと思いました:grinning:

参考にさせていただいたサイト

ApacheでPOSTデータをログに記録したい
Apache 2.4でPOSTの内容をログに出力するときは"LogLevel debug"だけじゃ出てこない
【sed / awk / grep】文字列の置換・抽出・検索と正規表現 | Linux Cheat Sheet

さいごに

いつもお世話になってばかりのQiitaに初めて投稿してみました:sunny:
記事に需要があるか分かりませんが、備忘録ということで…。
他に楽にPOSTが受付られる方法あればぜひ教えてください:heart_eyes:

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