我在 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 查询数据库。
有什么线索吗?
最佳答案
确实是 PUT/POST,而且:
@app.route('/v1/myapi/editperson/<int:id_person>)', methods=['PUT'])
对比
@app.route('/v1/myapi/editperson/<int:id_person>', methods=['PUT'])
关于python - PUT 请求返回 flask 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51167985/