我已经用python编写了一个简单的REST-ful web服务器,其中flask
遵循了此tutorial中的步骤;但我有一个问题叫POST
请求。该代码是:无法在烧瓶python中调用POST请求时解码JSON对象
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
if not request.json or not 'title' in request.json:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task': task}), 201
我用curl
如在上述页面的例子发送POST
要求:
curl -i -H "Content-Type: application/json" -X POST -d '{"title":"Read a book"}' http://127.0.0.1:5000/todo/api/v1.0/tasks
但我响应此错误:
HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Content-Length: 187
Server: Werkzeug/0.11.10 Python/2.7.9
Date: Mon, 30 May 2016 09:05:52 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)</p>
我试图调试,我发现在get_json
方法中,传递的参数已被翻译为'\\'{title:Read a book}\\''
为data
和request_charset
是None
;但我不知道解决方案。任何帮助?
编辑1:
我已经试过@ domoarrigato的答案,并实施了create_task
方法如下:
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
def create_task():
try:
blob = request.get_json(force=True)
except:
abort(400)
if not 'title' in blob:
abort(400)
task = {
'id': tasks[-1]['id'] + 1,
'title': blob['title'],
'description': blob.get('description', ""),
'done': False
}
tasks.append(task)
return jsonify({'task': task}), 201
但这次我通过curl
调用POST
后,得到了以下错误:
HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Content-Length: 192
Server: Werkzeug/0.11.10 Python/2.7.9
Date: Mon, 30 May 2016 10:56:47 GMT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
编辑2:
为了澄清,我应该提及,我正在使用Python版本2.7和最新版本的Flask开发64位版本的Microsoft Windows 7。
请查看我对该问题的更新。 –
更新了我的答案 - 在return语句中删除了',201' - 我想你只需要返回序列化的json。 – domoarrigato
不幸的是,它没有奏效。 –