Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ETags and collections

Many REST APIs provide the ability to search for resources.

For example, resources of type A may be fetched using the following HTTP request:

GET /A?prop1={value1}&prop2={value2}

I'm using optimistic locking and therefore would like to return a version for every returned resource of type A. Until now, I used the ETag header when fetching only one resource using its ID.

Is there an HTTP way for returning version for multiple resources in the same response? If not, should I include the versions in the body?

Thanks, Mickael


EDIT: I found on the web that the ETag is often generated by computing a hash of part of the reply. This approach fits well with my case since a hash of the returned collection will be computed. However, if the client decides to update one of the elements in the collection, which ETag should he put in the If-Match header? I'm thinking that including the ETags of the individual elements is the only solution...

like image 866
manash Avatar asked Feb 14 '15 18:02

manash


Video Answer


1 Answers

I would adopt one of these options:

  1. Make ETags weak by default and they are generated with the resource current state, not with the resource representation in the HTTP response payload. With that, I can return a valid ETag for each resource in the collection query response body, besides the ETag for the whole collection in the response header.

  2. Forget If-Match and ETags for this case and use If-Unmodified-Since with a Last-Modified supplied as a property of each resource. By doing that I can preserve the strong ETags, but clients can still make updates to one item based on the collection response without the need for another request to the resource itself.

  3. Allow updates via PATCH on the collection resource itself, using the If-Match header with the ETag for the whole collection. This probably won't work very well if there's a lot of concurrent changes, but it's a reasonable approach.

like image 121
Pedro Werneck Avatar answered Sep 28 '22 06:09

Pedro Werneck