python - PUT 请求返回 flask 错误

标签 python mysql curl flask flask-sqlalchemy

我在 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/

相关文章:

python - 如何在不改变尺寸的情况下减小 PNG 图像的文件大小?

mysql - 通过连接收集多列聚合数据

r - 开发R包需要处理 "SSL connect error"

python - 需要使用 Numpy 评估较大 3D 阵列中的较小 3D 阵列

python - scipy、fftpack 和 float64

python - 我置顶文章?但是当我开始我的项目时,我遇到了错误 unhashable type : 'slice'

MYSQL从另一个表引用表

java - 当运行可执行 jar 文件时,我有 SQLException : No suitable driver found for jdbc:mysql://localhost:3306/DENTAL

c++ - easy_curl_perform 返回错误 26(写入错误)

PHP 执行时间超过 max_execution_time... 有时