News for hackers. Stuff that matters.

REST question

I'll start by saying that I understand REST APIs. However, I am having trouble with one thing. How do typical methods - suspend, archive, export, etc - translate best to REST APIs?

For example, say I have a project management system. And you can archive a project. How should my REST API handle archival?

I assume you'd do a POST to myapp.com/project/1234, and you'd pass some data in like { archived: true }.

Or maybe a POST to: myapp.com/project/1234/archive. Although I guess that URL is not restful.

So what's the approach that most apps take here? A generate "update" action that takes varying parameters, rather than individual actions for each method, would be a bit of a pain to code and document, no?

( 6 comments | 6 points | on HN )
Re:REST question by jsarch on Tuesday December 07, 2010 @10:33PM (#1981755)
From what I can tell, I think it depends on if you want to be able to access the project later. If you don't, then I would recommend using DELETE /project/1234 with { archived: true} as an option. If you want to view the project later, then I think you should do a PUT to create the "archive" resource as in PUT /project/1234/archive or do a POST if you don't know the actual archive URI.
Re:REST question by rmk on Tuesday December 07, 2010 @09:26PM (#1981605)
Not necessarily, if you are using rails, you get update actions for free in your controller.

So you could do: curl -X PUT -d "<params>" http://myapp.com/project/1234

See section 11.6 (How to take snapshots of resources) in the book "RESTFul web services cookbook" for a discussion of this...

Re:REST question by stevepotter on Tuesday December 07, 2010 @10:18PM (#1981717)
I realize you get update actions. But what if you have different types of updates? Perhaps editing basic info like a project's Title. Or locking it? Or archiving? Are they lumped into the same action?
Re:REST question by rmk on Tuesday December 07, 2010 @10:49PM (#1981799)
No, you can actually generate different names for the actions that are part of url using rails routing, and you can require that they all use the HTTP PUT method.
Re:REST question by spooneybarger on Tuesday December 07, 2010 @10:26PM (#1981736)
you might get more and potentially better answers somewhere like stackoverflow.com
Re:REST question by steveklabnik on Tuesday December 07, 2010 @10:31PM (#1981753)
PUT to myapp.com/project/1234 with the param {"status":"archived"}, then set your model up so that when the status changes to archived, it archives itself.