DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 970,177 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in

Posted on

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

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 know...delete stuff.

A GET route should look like:

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:

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:

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:

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.

Oldest comments (0)

You’ve already scrolled down this far, why not join our community of 900k+ developers all learning together?