【问题标题】:request.get_json() return 400 bad requestrequest.get_json() 返回 400 错误请求
【发布时间】:2021-02-23 08:55:31
【问题描述】:

所以我正在测试 POST 以在邮递员中将新内容添加到我的数据库中,并且它一直返回 400 错误而没有其他信息

127.0.0.1 - - [23/Feb/2021 00:46:04] "[1m[31mPOST /drinks HTTP/1.1[0m" 400 -

我尝试使用 request.is_json,它返回 True。代码只要运行到就返回错误

new_drink = request.get_json()

我不知道我在哪里搞砸了。

@app.route('/drinks',methods=['POST'])
@requires_auth('post:drinks')
def add_drinks(payload):
    new_drink = request.get_json()
    title = new_drink.get('title')
    recipe = new_drink.get('recipe')
    drink = Drink(title=title,recipe=json.dumps(recipe))
    drink.insert()
    result = {
        "success": True,
        "drinks": drink.long()
    }
    return jsonify(result), 200

这是我的项目的链接

https://github.com/TYL1026/coffee_shop/blob/main/backend/src/api.py

【问题讨论】:

  • 您应该再次检查您的 Postman 设置。 Http 400 表示错误来自客户端请求端。

标签: python-3.x flask postman flask-sqlalchemy


【解决方案1】:

正如 cmets 中所述,在我的情况下,问题是标题。我正在设置"Content-Type": "application/json",但我正在发送表单数据。我删除了标题,问题就解决了。

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 2019-07-11
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多