Home > OS >  REST on non-CRUD operations
REST on non-CRUD operations

Time:03-23

I have a resource called “subscriptions”

I need to update a subscription’s send date. When a request is sent to my endpoint, my server will call a third-party system to update the passed subscription.

“subscriptions” have other types of updates. For instance, you can change a subscription’s frequency. This operation also involves calling a third-party system from my server.

To be truly “RESTful,” must I force these different types of updates to share an endpoint?

PATCH subscriptions/:id

I can hypothetically use my controller behind the endpoint to fire different functions depending on the query string... But what if I need to add a third or fourth “update” type action? Should they ALL run through this single PATCH route?

CodePudding user response:

To be truly “RESTful,” must I force these different types of updates to share an endpoint?

No - but you will often want to.

Consider how you would support this on the web: you might have a number of different HTML forms, each accepting a slightly different set of inputs from the user. When the form is submitted, the browser will use the input controls and form metadata to construct an HTTP (POST) request. The target URI of the request is copied from the form action.

So your question is analogous to: should we use the same action for all of our different forms?

And the answer is yes, if you want the general purpose HTTP application to understand which resource is expected to change in response to the message. One reason that you might want that is cache invalidation; using the right target URI allows all of the caches to understand which previously cached responses should not be reused.

Is that choice free? no - it adds some ambiguity to your access logs, and routing the request to the appropriate handler in your code takes a bit more work.


Trying to use PATCH with different target URI is a little bit weird, and suggests that maybe you are trying to stretch PATCH beyond the standard constraints.

PATCH (and PUT) have remote authoring semantics; what they mean is "make your copy of the target resource look like my copy". These are methods we would use if we were trying to fix a spelling error on a web page.

Trying to change the representation of one resource by sending a remote authoring request to a different resource makes it harder for the general purpose HTTP application components to add value. You are coloring outside of the lines, and that means accepting the liability if anything goes wrong because you are using standardized messages in a non standard way.


That said, it is reasonable to have many different resources that present representations of the same domain entity. Instead of putting everything you know about a user into one web page, you can spread it out among several that are linked together.

You might have, for example, a web page for an invoice, and then another web page for shipping information, and another web page for billing information. You now have a resource model with clearer separation of concerns, and can combine the standardized meanings of PUT/PATCH with this resource model to further your business goals.

We can create as many resources as we need (in the web level; at the REST level) to get a job done. -- Webber, 2011


So, in your example, would I do one endpoint like this user/:id/invoice/:id and then another like this user/:id/billing/:id

Resources, not endpoints.

GET /invoice/12345
GET /invoice/12345/shipping-address
GET /invoice/12345/billing-address

Or

GET /invoice/12345
GET /shipping-address/12345
GET /billing-address/12345

The spelling conventions that you use for resource identifiers don't actually matter very much.

So if it makes life easier for you to stick all of these into a hierarchy that includes both users and invoices, that's also fine.

  •  Tags:  
  • rest
  • Related