TIL

Today I Learned. 知ったこと、学んだことを書いていく

リクエストのJSONデータを受け取る - Bottle

リクエストでJSONを送信してきた場合の処理

from bottle import route, run, request

@route("/hello")
def hello():
    contentType = request.get_header('Content-Type')
    print(f"header Content-Type: {contentType}")
    if contentType == "application/json":
        pprint(request.json)

if __name__ == "__main__":
    run(host="localhost", port=3000, debug=True)
  • request.get_header()でheaderのContent-Typeがapplication/jsonであるか確認
  • request.jsonでデータを受け取る

参考文献

API Reference - Bottle 0.13-dev documentation

Tutorial - Bottle 0.13-dev documentatio