Sunday 2 December 2012

Understanding REST In Practical Terms

I have been using Rails for a little while now and REST is always floating around conversations about designing things in Rails. It seemed like a nebulous topic so I did a little research of my own.

To my understanding it is all about building resources and nouns that respond to the HTTP verbs.

Instead of sending a GET request to http://www.example.co.uk/user/create , you PUT to http://www.example.co.uk/user
Similarly you POST to user/151 if you wish to update him.

It really is that simple.

There is one Gotcha though. If you want to delete user 151, you send a DELETE request to /user/151, you can then perfectly validly send another DELETE request to /user/151 because it is a word new to me, Idempotent, which roughly means you can do the same request to the same noun and the system will not change.
If I were to PUT (create) the same user twice, it is also idempotent and should only have the first user assuming you have some valid way of identifying duplicates.