In scientific research there is principle called Occam’s razor. The term states that the simplest solution is usually the best one. This concept is especially true when it comes to application routing. In this guide we’ll go through a RESTful routing tutorial, including a walk through of a real world application.
What is RESTful Routing?
Since this is a Manageable Monday episode, the goal is to give a dead simple explanation for what REST is. And more importantly, how you can use it as a developer.
So what exactly is RESTful routing? REST stands for Representational State Transfer. The concept of REST was originally created by the computer scientist Roy Fielding for his dissertation at UC Irvine.
If that doesn’t make any sense, don’t worry. You don’t have to understand the intricacies of REST in order to use it.
Let’s begin with talking about the communication system that REST uses: the HTTP protocol. HTTP is what you use when you:
- Navigate on the Internet
- Submit forms
- Or even look at Yelp restaurant reviews on your phone
One of the issues with HTTP is that it is stateless. Which means that when you navigate from one page to another page, HTTP doesn’t retain any of the information between pages.
This means that developers need to implement code to let applications know how to act like they know what’s going on.
Dead Simple REST Examples
Remember when I mentioned Occam’s razor at the beginning of the guide? That principle came to mind when I was researching this post because at its core RESTful routing is the definition of simplicity.
Essentially REST is a naming structure that encompasses every action that users and applications need to make.
So what are the terms? Let’s go through them one by one and give a real world example of how they’re used. After going through it you’ll see that everything you do on the web can be boiled down to five tasks. I’m going to use the example of using Twitter to illustrate what each term does.
|Formal Term||Action||Practical Example|
|POST||Create||Creating a new Tweet|
|GET||Read||Seeing a single or multiple Tweets|
|PUT||Update/Replace||Replacing a Tweet|
|PATCH||Update/Modify||Editing a Tweet|
|DELETE||Delete||Deleting a Tweet|
If you think about each of those behaviors, you can derive every action that you or an API takes on the web.
Why is REST Important?
So why exactly is REST important? It seems so straightforward that many developers forget what it was like before RESTful routing became popular. Years ago application routes were dictated by the developer. This may not seem like a big deal. Except that it meant that every application had a different naming structure for routes.
Having unique names for routes was not a scalable solution. Thankfully REST was created to bring a level of standardization to development. By leveraging RESTful routes you can immediately be familiar with how data will flow through an application.
The concept of REST was so helpful to the developer community that it was embraced by the most popular frameworks in the space. When it comes to modern application or API development you’ll discover that the REST architecture is the primary routing system utilized.
RESTful Routing Tutorial
Let’s walk through what this looks like in a real world application. Here is a basic Ruby on Rails application that has a single feature: managing tasks. This project is taken from my Learn Ruby on Rails from Scratch Course if you want to check it out.
If you look at the routes you’ll see that the Rails framework provides for each of the RESTful routes we discussed.
In the red box I designate all of the routes for the app’s
Project feature. In the yellow box I highlighted each of the RESTful verbs. Lastly, in blue I show where each of the HTTP actions are mapped to.
For example, notice how the features that revolve around showing pages, such as
show are all GET requests? This is because the server is looking up information in the application or database and GETTING resources to show the user.
This is different from the action such as POST that actually
creates a new item in the database.
I hope that this has been a helpful RESTful routing tutorial. And that you now feel more confident in understanding how you can use REST to build your own applications.