将“put”与 Flask API 和 JSON 一起使用时出现错误 405

问题描述 投票:0回答:1

我对编程和 json 相当陌生。当我尝试通过“app.py”连接到我的 API 并上传预订时,我不断收到错误 405。

在我的 main.py 中我有:

def add_new_booking(name, animal_type):
    booking = {
         "customer_name": name,
         "animal": animal_type
    }

    result = requests.put(
        'http://127.0.0.1:5001/booking',
        headers={'content-type': 'application/json'},
        data=json.dumps(booking),
    )
    return booking  

在我的 app.py 中我有:

@app.route('/booking', methods = ['PUT'])
def book_feed():
    booking = request.json()
    find_class_id(animal=booking['animal'])
    spaces_left(animal=booking['animal'])
    add_booking(
        customer_name=booking['customer_name'],
        animal=booking['animal'],
    )
    remove_space(find_class_id(animal=booking['animal']))

    return booking

有修改数据库的功能。我尝试更改为 get 但似乎无法将预订获取到 API。任何帮助表示赞赏!

我尝试更改为“获取”和“发布”,但似乎无法将预订获取到 API。我对动物数据有不同的要求,效果很好。任何帮助表示赞赏!

python json flask put
1个回答
0
投票

如果您使用 Flask,当您的服务器运行

book_feed
函数时,您应该会看到以下错误:

.
.
.
  File "...server.py", line 8, in book_feed
    booking = request.json()
              ^^^^^^^^^^^^^^
TypeError: 'dict' object is not callable

request.json
属性是字典,而不是函数。你可以写:

booking = request.json

(或者您可以直接参考

request.json
而不是
booking
。)

© www.soinside.com 2019 - 2024. All rights reserved.