简体   繁体   中英

PUT request is returning an error with flask

I use Flask in an API application and I have this function:

@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)

When I execute this curl request:

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

I get this error:

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"
}

I do have a row in the database with the id = 38 so I don't know why the request doesn't find it. I use SQLAlchemy to query the database.

Any clues?

Indeed PUT/POST but also:

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

vs.

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

Your annotation specifies that this API just allow PUT method. But you request this API through POST method. Try:

curl -X PUT ....

您的路线通过了id_person,但您的查询使用了person_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM