我正在尝试在 Flask 中制作一个简单的 api,第一步是获取 POST json 数据。(我现在只想打印它)这是我的代码,当我使用 json 数据请求 /api 时,它返回 500 错误。关于为什么会发生这种情况的任何想法?
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 -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>
身体.json:
{
"please": "print",
"me": "now"
}