在flask python中调用POST请求时无法解码JSON对象 [英] Got Failed to decode JSON object when calling a POST request in flask python

查看:50
本文介绍了在flask python中调用POST请求时无法解码JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 flask 在 python 中编写了一个简单的 REST-ful web 服务器,按照这个 教程;但是我在调​​用 POST 请求时遇到了问题.代码是:

I have written a simple REST-ful web server in python with flask following steps in this tutorial; but I've got a problem calling POST request. The code is:

@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 请求作为上述页面中的示例:

I send a POST request using curl as the example in the above mentioned page:

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

但我收到此错误作为回应:

But I get this error in response:

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}\\'' 作为 datarequest_charsetNone;但我不知道解决方案.有什么帮助吗?

I've tried to debug and I found out in the get_json method, the passed argument has been translated to '\\'{title:Read a book}\\'' as data and request_charset is None; but I have no idea for a solution. Any help?

编辑 1:

我尝试了@domoarrigato 的回答并实现了 create_task 方法如下:

I have tried @domoarrigato's answer and implemented the create_task method as the following:

@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后得到以下错误:

But this time I got the following error after calling POST via curl:

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.

To clarify, I should mention that I'm working on a 64-bit version of Microsoft Windows 7 with Python version 2.7 and the latest version of Flask.

推荐答案

这适用于 Windows 7 64 位:

This works in Windows 7 64:

curl -i -H "Content-Type: application/json" -X POST -d "{\"title\":\"Read a book\"}" http://localhost:5000/todo/api/v1.0/tasks

反斜杠和双引号.

这篇关于在flask python中调用POST请求时无法解码JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆