restpostput

POST or PUT to update all records of table?


In a REST context, should POST or PUT be applied if the database ends up calling a method to set field1 to zero in all records of table1?


Solution

  • In a REST context, should POST or PUT be applied if the database ends up calling a method to set field1 to zero in all records of table1?

    In theory, it could be either, but in practice you'll probably want to use POST.

    Remember, PUT and POST methods are to be understood from the perspective of the resource abstraction -- your database is just an implementation detail hidden behind the facade that makes your system behave like a web site.

    So if you have a resource that includes a list of all of your field1's, and somebody edits their local copy to zero out all the fields, then PUT would be a reasonable way to ask that the server's copy be changed to match this client's local copy.

    But in practice, it is more likely that you'll instead want to have the client send a "resource, zero out field1 please" message, and the correct HTTP method to use in this scenario is POST:

    The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.