烧瓶错误请求错误 400

人类

我是 Flask 开发的新手。下面发布了我现在正在关注的教程系列的请求。我使用 Postman 发出一个 post 请求,如下所示。但是当它运行时,我会显示一个错误控制台:

浏览器(或代理)发送了一个请求,该服务器无法

 `from flask import Flask, jsonify, abort, make_response, request

    app = Flask(__name__)

    tasks = [
        {
            'id': 1,
            'title': u'Buy groceries',
            'description': u'Milk, Bread, Pizza, Fruit',
            'done': False
        },
        {
            'id':2,
            'title':u'Learn Python',
            'description': u'Need to find a good Python tutorial on the web',
            'done': False
        }
    ]

    @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': request.json['task']['id'],
            'title': request.json['task']['title'],
            'description': request.json['task']['description'],
            'done': False
        }
        print(request.json)
        tasks.append(task)
        return jsonify({'task': task}), 201


    if __name__ == '__main__':
        app.run(debug=True)`

如果你能帮助我理解错误的原因,我会很高兴。json请求是:

{
  "task": {
        "id": 3,
        "title": "Read a book",
        "description": "",
        "done": false
  }
}
约翰·萨克迈斯特

我相信这条线失败了:

    if not request.json or not 'title' in request.json:
        abort(400)

'title'在 下'task',所以你可能想要这样的东西:

if not request.json or 'task' not in request.json or 'title' not in request.json['task']:
    abort(400)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章