3

Let's say I have built a REST service for making notes that looks something like this:

GET    /notes/     // gives me all notes
GET    /notes/{id} // gives the note with the identified by {id}
POST   /notes/     // creates note
PUT    /notes/{id} // updates note identified by {id}
DELETE /notes/{id} // delete note

now I want to create subresources that allows me to get/update/delete the first or last note.

GET    /notes/first // get first note added
PUT    /notes/last  // updates last note added
DELETE /notes/first // delete first note

Does this go against the principles of REST?

Thank you in advance :)

EDIT:

PS: In this example i expect the ID to be always a number

Zounadire
  • 1,496
  • 2
  • 18
  • 38

1 Answers1

2

In my humble opinion this is perfectly legal and desirable. I especially like the semantics of DELETE /notes/first, but also consider POST /notes/first - put a new note before the first one.

In your case first and last are some sort of special magic identifiers or placeholders. On the other hand what are the alternatives?

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Hi Tomasz, thank you for your answer. I like your POST /notes/first consideration, allows it to use the service in a stack like fashion. Regarding that what would be the best way to move a certain note to first position? – Zounadire Feb 21 '12 at 16:30
  • @Zounadire: good question! My idea is `POST /notes/42/move` or a destructive pair of `DELETE` and `POST /notes/first` :-( – Tomasz Nurkiewicz Feb 21 '12 at 16:35
  • Hmmm... Ok, but this of course only works if there is an additional field to track the positions in the list, since I want to keep the id the same so that a certain note is always reachable under the same url. That's why I see a problem with the second approach in cases where the server is responsible for generating the ids: in this case you can't guarantee that the note will have the same id after reposting the note. – Zounadire Feb 21 '12 at 16:53
  • @Zounadire: yes, also if note has some relationships you must be sure to preserve them when deleting and recreating. That's why it's a poor idea. So another shot: `PUT /notes/42/moveToFirst` - I changed `POST` to `PUT` - this method is idempotent. – Tomasz Nurkiewicz Feb 21 '12 at 16:56
  • 1
    Do you see any problems when swapping the positions of two elements? For example: `PUT /notes/42/swap/24` or `PUT /notes/last/swap/first` – Zounadire Feb 21 '12 at 17:18
  • @Zounadire: well, it is really a matter of preference, looks fine to me (but it should be `POST` in this case). I also though about `/notes/swap/first/last`, but your URI pattern looks better (?) – Tomasz Nurkiewicz Feb 21 '12 at 17:23