背景
webhookなどの自作スクリプトの動作確認などをする際に、 httpリクエストの対応相手側が受け取った情報を確認できるようにしたい
動作要件
python3/flask (動作確認は、Python 3.7.3@Macbookpro2019で行った)
事前準備
$ pip3 install Flask
$ python3 -m pip install Flask
スクリプト
from flask import Flask, request
app = Flask(__name__)
@app.route("/", methods=['POST'])
def webhook():
headers = request.headers
print(headers)
body = request.get_data().decode('utf-8') + '\n'
print(body)
return body
if __name__ == "__main__":
app.run()
起動方法
スクリプトを実行するのみ。終了はCtrl+C
※環境に依っては、pythonの部分がpython3だったりpytohn3.xだったりします。
$ python test.py
* Serving Flask app "test" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
動作確認
別ターミナルを開いて、確認。確かにBODY部を応答している。 以下では、print(herders)をコメントアウトしてBODYのみ表示している
$ curl -X POST -d $(echo "hello") http://127.0.0.1:5000/
hello