前提
flask APIをpythonにて構築し、flaskが起動している時に下記のコマンドを送信しました。
curl -X POST http://127.0.0.1:5000/detect -H "Content-Type: application/json" -d '{"filename": "xxx.jpg"}'
すぐに下記のエラーメッセージが発生しました。
<!doctype html> <html lang=en> <title>400 Bad Request</title> <h1>Bad Request</h1> <p>Failed to decode JSON object: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)</p>
実現したいこと
pythonで構築したflask APIにて下記の様なjsonが返ってきて欲しいです。
response : {"camera": 12, "lens":100}
最小限のコードを記述します。
ディレクトリがapiの中にあるflask_test.pyを呼び出しています。
__init__.py
1from flask import Blueprint, jsonify, request 2 3from api import flask_test 4 5 6api = Blueprint("api", __name__) 7 8 9@api.get("/") 10def index(): 11 return jsonify({"column": "value"}), 201 12 13@api.post("/detect") 14def detection(): 15 return flask_test.detection(request) 16
flask_test.py
1 2def load_image(request): 3 print('----') # curlを打ち込みますとコンソールに表示されます。 4 filename = request.json["filename"] 5 print(filename) # コンソールに表示されません。 6 7 8def detection(request): 9 print("test") # 表示されます。 10 load_image(request) 11
flaskのblueprintによる起動コードを書きます。
app.py
1 2import os 3from flask import Flask 4 5from api import api 6 7 8class Config: 9 TESTING = False 10 DEBUG = False 11 LABELS = [ 12 "people", 13 "Population" 14 ] 15 16 17class LocalConfig(Config): 18 TESTING = True 19 DEBUG = True 20 21 22config = { 23 "base": Config, 24 "local": LocalConfig, 25} 26 27config_name = os.environ.get("CONFIG", "local") 28 29app = Flask(__name__) 30app.config.from_object(config[config_name]) 31 32app.register_blueprint(api) 33 34 35# @app.route("/") 36# def index(): 37# return "Hello" 38
起動後、ポート5000には、
{
"column": "value"
}
が表示されます。
その起動中に上記のcurlコマンドを打ち込みますと、Bad Requestとなります。
下図は、リクエストした後のターミナル画面を表した画面です。
コンソール画面です。
testのあと、----が表示されますが、その後のprint文 print(filename) が表示されません。
この様な結果、request.json["filename"] が原因箇所となりますが対策方法が見つかっておりません。
ご指導の程よろしくお願いします
補足情報(FW/ツールのバージョンなど)
OS: WIN10
python: 3.9.13
flask: 2.2.2
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/17 07:08
2022/11/17 13:26
2022/11/17 15:01
2022/11/18 08:33
2022/11/28 16:02