Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Marking as read" in a RESTful application

Tags:

rest

I am writing a site in which I wish to mark pages as read once they have been, but cannot figure out the most RESTful way of doing so.

Obviously to retrieve the page the client will send a GET request, but if I then go and mark the page as read this breaks the rule that GET should be idempotent - it's now changing something on the server.

Is there a better way to do this?

like image 727
tsvallender Avatar asked Feb 11 '11 18:02

tsvallender


1 Answers

Is the read status an inherent part of the resource, or is it just a server-side tracking facility? Would readers want to get lists of all the documents that are 'read' or 'unread'? Will users be able to mark a document as 'unread' if they download it but decide to read it later?

If it's just server-side, then it's fine to mark a {user,resource} tuple as read on each GET request. If the per-user read status is part of the resource, you need a client-writable many-to-many mapping between users and resources, and either the pair are mapped (read) or not (unread). In this case, I suggest using a new resource to represent this status, and allowing the client to PUT a true/false boolean to that resource URI. Be it user@host/resource/read or /user/{user}/resource/read or some other, it doesn't really matter.

If you want this flag to be set automatically, and your client is a web browser, you will need to use AJAX to do a second request once the document is downloaded (and perhaps the tab has been visible for a specific elapsed period).

like image 167
Nicholas Shanks Avatar answered Nov 02 '22 05:11

Nicholas Shanks