16
我想在Flask中做一個簡單的API,第一步是獲取POST JSON數據。 (我只想現在打印它)這是我的代碼,當我用json數據請求/ api時,它返回一個500錯誤。任何想法爲什麼發生這種情況?Flask POST請求導致服務器崩潰
from flask import Flask, request, Response
app = Flask(__name__)
@app.route('/')
def root_response():
return "Hello World."
@app.route('/api', methods=['POST', 'GET'])
def api_response():
if request.method == 'POST':
return request.json
if __name__ == '__main__':
app.run()
curl命令:
$ curl -H "Content-Type: application/json" --data @body.json http://127.0.0.1:5000/api
<!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>
body.json:
{
"please": "print",
"me": "now"
}
該調試模式爲我節省了很多時間:D – 2015-06-15 07:40:43
調試的其他設置,因爲我發現,儘管調試設置爲True,我沒有得到屏幕上的錯誤消息。 – disruptive 2015-09-02 17:53:27
我實際上已經意識到所有的錯誤信息都被輸出到我運行服務器的終端。然而,調試模式證明對其他測試有幫助(並且它可以在你運行curl命令的地方打印出來) – alisa 2017-01-17 19:35:25