前提・実現したいこと
JSON形式で出入力するAPIを実装していました。
curlでJSON形式のデータを受け取る処理を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>400 Bad Request</title> <h1>Bad Request</h1> <p>The browser (or proxy) sent a request that this server could not understand.</p>
該当のソースコード
python
1import json 2from flask import Flask, jsonify, make_response, request, Response 3 4app = Flask(__name__) 5 6@app.route('/post', methods=['POST']) 7def post_json(): 8 json = request.get_json() # Get POST JSON 9 NAME = json['name'] 10 result = { 11 "data": { 12 "id": 1, 13 "name": NAME 14 } 15 } 16 return jsonify(result) 17 18if __name__ == "__main__": 19 app.run()
試したこと
request.get_json()の部分をrequest.data.decode()に変更すると、一応動きました。
しかし、json.loads()を使って、文字列をJSON形式に直そうとすると以下のようなエラーになります。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>500 Internal Server Error</title> <h1>Internal Server Error</h1> <p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
補足情報
バージョンは下記の様になっています。
Python 3.7.2 Flask 1.0.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/02 01:28