Flask 未从 HTTP POST 接收数据
Flask Not Receiving Data from HTTP POST
我正在研究我 API 中的第一个 POST 方法路线。出于某种原因,当我通过 url http://test.com/test?stuff=wee 将变量传递给它时,Flask 会抛出错误,比如东西没有通过。
代码:
@app.route('/test', methods = ['POST'])
def testindex():
stuff = request.args.get('stuff')
response_message = "success"
response_data = stuff
errors = "None"
response = {
'response message' : response_message,
'response data': response_data,
'errors' : errors
}
js = json.dumps(response)
resp = Response(js, status=200, mimetype='application/json')
return resp
卷曲:
curl -H "Content-Type: application/json" -X POST -d '{"stuff":"wee"}' http://test.com/test?stuff=wee
回复:
{"errors": "None", "response data": null, "response message": "success"}
帮忙?
request.args.get
用于从 GET 中获取参数。你会想要 request.form
.
我正在研究我 API 中的第一个 POST 方法路线。出于某种原因,当我通过 url http://test.com/test?stuff=wee 将变量传递给它时,Flask 会抛出错误,比如东西没有通过。
代码:
@app.route('/test', methods = ['POST'])
def testindex():
stuff = request.args.get('stuff')
response_message = "success"
response_data = stuff
errors = "None"
response = {
'response message' : response_message,
'response data': response_data,
'errors' : errors
}
js = json.dumps(response)
resp = Response(js, status=200, mimetype='application/json')
return resp
卷曲:
curl -H "Content-Type: application/json" -X POST -d '{"stuff":"wee"}' http://test.com/test?stuff=wee
回复:
{"errors": "None", "response data": null, "response message": "success"}
帮忙?
request.args.get
用于从 GET 中获取参数。你会想要 request.form
.