Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How handle PATCH method in Flask route as API?

Tags:

python

flask

I have this route:

@bp.route('coordinates/<int:id>/update', methods=['PATCH'])
def update_coordinates(id):
    schema = CoordinatesSchema()
    coords = Coordinates.query.get_or_404(id)
    new_data = request                           #????

    # some another logic
    return jsonify({"result": "GOOD"}), 200

I am passing a data for update in a body, like a dict: { "title": "newtitle"} but I how can I get this info inside a route?

like image 673
Chiefir Avatar asked Mar 06 '26 12:03

Chiefir


1 Answers

With PATCH requests you retrieve request data the same way you do for every other request type (e.g. POST). Depending on how you send your data, there are several ways to retrieve it:

Sending as application/json:

data = request.json

Sending as application/x-www-form-urlencoded (form data)

data = request.form

Sending as raw body with no Content-Type header:

data = request.data

The last one will give you a bytes string that you will then have to process accordingly. For your use case I suggest using the first example and add a Content-Type: application/json header when you send your PATCH request.

like image 147
dmitrybelyakov Avatar answered Mar 09 '26 01:03

dmitrybelyakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!