LoginSignup
9
4

More than 5 years have passed since last update.

FlaskがCurlからPOSTされたJSONデータを読んでくれない時

Last updated at Posted at 2014-11-26

ダメな例

$ curl -X POST -d '{"name": "fuga"}' localhost:8080/v1/target

これだと


@app.route('/v1/target', methods=['POST'])
def new_machine():
    print(request.data)  # => ''

何も出力してくれなくて延々と頭を悩ますことになります。(なりました

正しくはこう

$ curl -H "Content-type: application/json" -X POST -d '{"name": "fuga"}' localhost:8080/v1/target

これが付いてないと文字列としてすらデータを受け取らないようです。

あとあんまり居ないと思うけどWindowsから使ってる人は{}をちゃんとエスケープしないと辛いらしいですよ。

$ curl -H "Content-type: application/json" -X POST -d '\{"name": "fuga"\}' localhost:8080/v1/target
9
4
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
9
4