DEV Community 👩‍💻👨‍💻


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.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Just kidding, it's a personal preference. But you can change your theme, font, etc. in your settings.

The more you know. 🌈