Home > Net >  What HTTP method type should be used for recalculating the related resources
What HTTP method type should be used for recalculating the related resources

Time:02-10

In my application I maintain couple of Shapes. And these shapes are calculated with respect to some reference point. And this reference point is considered as (0, 0). For example someone wants to move this reference point by (x, y), then distance of all existing Shapes need to be re-calculated with new reference point.

I have an REST API, to change this reference point, so internally all the Shapes distance is re-calculated with that (x, y) movement.

What HTTP method should be used for such operation POST, PUT or PATCH? As I don't find any one of these correct in the context, if I go by the definition of these HTTP method.

CodePudding user response:

What HTTP method should be used for such operation POST, PUT or PATCH? As I don't find any one of these correct in the context, if I go by the definition of these HTTP method.

It depends.

We use PUT/PATCH when the client edits a local copy of a resource, and sends the edited version of the document back to the server (aka "remote authoring"). Think "Save File".

We use POST when... well, pretty much for everything else.

POST serves many useful purposes in HTTP, including the general purpose of “this action isn’t worth standardizing.” -- Fielding, 2009

Think HTML forms here - information is dispatched to the origin server in a representation that is completely unrelated to the representation of the resource itself.


For example, in a resource model where we have a resource for the reference point itself

GET /example
200 OK
Content-Type: appplication/json

{ "referencePoint": { "x": 0, "y": 0 } }

The using remote authoring semantics is pretty reasonable

PUT /example
Content-Type: appplication/json

{ "referencePoint": { "x": 0, "y": 999 } }

If the representation of the resource were very big (much bigger than the HTTP headers), and the changes were small, then we might use a patch document to communicate the information, rather than a complete document.

PATCH /example
Content-Type: application:json-patch json

[ { "op": "replace", "path": "referencePoint/y", "value": 999 } ]

In both cases, the basic idea is the same - we are requesting that the origin server change its copy of the resource to match the edited version on the client.


It is okay to use POST for everything else.

  • Related