简体   繁体   English

烧瓶错误请求错误400

[英]flask bad request error 400

I am new in Flask development. 我是Flask开发的新手。 Below is posted a request from tutorial series which I am following now. 以下是我正在关注的一系列教程的要求。 I used Postman to make a post request as shown in below. 我使用Postman发出了一个发帖请求,如下所示。 But when it runs I show an error console that: 但是,当它运行时,我会显示一个错误控制台:

The browser (or proxy) sent a request that this server could not 浏览器(或代理)发送了一个请求,要求该服务器无法

 `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)`

I will be happy if you can help me to understand the reason for error. 如果您能帮助我理解错误原因,我将非常高兴。 The json request is: json请求是:

{
  "task": {
        "id": 3,
        "title": "Read a book",
        "description": "",
        "done": false
  }
}

I believe this line is failing: 我相信这条线是失败的:

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

'title' is under 'task' , so you probably want something like: 'title''task' ,因此您可能想要类似以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM