我正在Python中使用Flask實現REST API。 我必須獲取參數來執行查詢並返回資源。爲了與REST
原則保持一致,我將使用GET
請求進行此操作。如何在Flask中獲取GETs json數據
既然可以有很多的參數,我想通過一個conf.json
文件給他們,例如:
{"parameter": "xxx"}
我通過curl
執行請求:
$捲曲 - H「Content-Type:application/json」--data @ conf.json -G http://localhost:8080/resources/
該請求是redire反恐執行局的路線與這些操作:
@resources.route('/resources/', methods=['GET'])
def discover():
if request.get_json():
json_data=request.get_json()
return jsonify(json_data)
我得到的回覆是:
<head>
<title>Error response</title>
</head>
<body>
<h1>Error response</h1>
<p>Error code 400.
<p>Message: Bad request syntax ('GET /resources/?{"parameter": "xxx"} HTTP/1.1').
<p>Error code explanation: 400 = Bad request syntax or unsupported method. </body>
有人知道如何獲得JSON數據並妥善請求處理呢?
@加載文件的URL參數,因爲該請求是GET類型。這看起來不太好,可能會導致字符編碼的許多問題。你確定這是你想要的嗎? – Fma