Search code examples
pythonflaskhttprequestopenapi

Which HTTP method to use when no data is transfered, just to execute code in an endpoint?


I build an Flask API endpoint which when called will execute an action to process some data (the data are not send but come from the database). The result of the method(action) called is to update some fields in some database objects based on calculations performed on the objects themselves.

Initially from what I found most people use POST requests, but post request is mainly designed for sending data in order to create a resource and in these cases no such thing takes place.

I looked at all the possible HTTP request methods and no one fits the bill of just asynchronously executing some code in the server without sending and returning any data.

Am I missing something, should I simply use POST or is there a better alternative?


Solution

  • PATCH seems to be the correct method for your use case since you're updating some fields of a resource, although POST should also be fine.

    You can use 204 status code (which means api was successfully executed but there's no content to send) if you're updating the fields before api return. Refer this: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/204

    But if you're updating the fields async after api return, you should send 202 status code (which means request was accepted but not yet processed): https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/202