PUT请求返回flas错误

2023-09-22 19:03:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在API应用程序中使用Flask,我有以下功能:

@app.route('/v1/myapi/editperson/<int:id_person>)', methods=['PUT'])
def edit_person(person_id):

    req_data = request.get_json()
    firstname = req_data.get('firstname')
    lastname = req_data.get('lastname')
    session.query(model.Persons).filter(model.Persons.id==person_id).update( firstname, lastname)
    session.commit()
    session.close()     
    return jsonify(req_data)

当我执行这个curl请求时:

 curl -i -H "Content-Type: application/json" -X PUT -d '{"firstname": "myfirstname", "lastname": "mylastname"}' http://localhost:5000/v1/myapi/editperson/38

我得到这个错误:

HTTP/1.0 404 NOT FOUND
Content-Type: application/json
Content-Length: 27
Server: Werkzeug/0.14.1 Python/2.7.6
Date: Wed, 04 Jul 2018 07:17:09 GMT
Proxy-Connection: keep-alive
Connection: keep-alive

{
  "error": "Not found"
}

我在数据库中有一行id=38,所以我不知道为什么请求找不到它。 我使用SQLAlchemy查询数据库。你知道吗

有什么线索吗?你知道吗


Tags: idjsondatagetmodelputsessioncontent
3条回答

确实是PUT/POST,但也包括:

@app.route('/v1/myapi/editperson/<int:id_person>)', methods=['PUT'])

@app.route('/v1/myapi/editperson/<int:id_person>', methods=['PUT'])

您的注释指定这个API只允许PUT方法。但是您可以通过POST方法请求这个API。尝试:

curl -X PUT ....

您的路由通过id\u person,但您的查询使用person\u id

相关问题 更多 >