loading...

Restful routes or why do I have to do this?!

swishyfishie2 profile image Swishyfishie ・2 min read

Introduction:
REST or Representational State Transfer is an architectural style, a convention that enables interaction through descriptive HTTP requests : GET, POST or DELETE and makes it simpler to organize your app structure.

In order to understand how to use RESTful routes better, we need to take a look n what those HTTP requests mean.

GET is used to retrieve data and nothing else. It's like you'd play "get" with your HTTP dog...

POST is used when you want to make something new. Basically you send data to the database and the models, where it's processed and manipulated in the desired way. Nothing to do with posting mail though...

PATCH. Come on you two, patch things up! .. This one updates whatever you want to be updated.

DELETE is generally used to...you know...delete stuff.


A GET route should look like:
www.mywebsite.com/user/3
www.mywebsite.com/items/3/edit
www.mywebsite.com/products/new

They all should only display the content and not process it in any way. That's a job for the other routes.


A POST route should look like:
www.mywebsite.com/products

You might wonder why is this looking like a GET route, well, it just LOOKS like it, but it behaves differently. Whilst a GET request will only display a form, a POST request will process the input values and enable CRUD actions.


A PATCH route should look like:
www.mywebsite.com/items/3

Again, you'd think that we need the "/edit" but we don't. That '/edit' is only needed in the GET request, when we're displaying the updating form.

What PATCH does, it updates our (let's say) Item object, only with the specific parameters we're asking for, it doesn't recreate the whole object like a PUT request would do.


A DELETE route should look like:
www.mywebsite.com/items/3

Here we're deleting the specified source from the server. Nothing to see, move along!


You can see that we're following a clean pattern of routes here. A convention that is explicit to the user, that is easier to debug, easier to follow and also understand as there's a lot of documentation on it.

Posted on by:

swishyfishie2 profile

Swishyfishie

@swishyfishie2

Full-stack developer. I like rubies and javascripties.

Discussion

markdown guide