Intro to REST: Software Engineering 13 Phase 4 Hybrid

Learning Goals


Key Vocab


Introduction

In 2000, Roy Fielding was frustrated by the haphazard ways in which web applications were using HTTP. Specifically, he was frustrated with how URLs and their corresponding HTTP verbs were used differently for every single application. So, in his Ph.D. dissertation Links to an external site., he came up with REST n (RE presentational S tate Transfer) as a standardized way for web applications to structure their URLs.

Fielding also noticed the rise in web applications communicating with each other (What is an API?Links to an external site.). He hoped that inter-application communication would get much easier if there was a standard way of forming URLs to access resources.

If you have been building applications for a while, there is a good chance that you have already worked with RESTful APIs. json-server follows REST conventions very strongly. If your application posts to Twitter, pulls in a feed of images from Instagram, or calls a list of locations from Google Maps, you are using a RESTful API to communicate between applications.

What is REST?

REST is an architectural design pattern, not a framework or code in itself.

Many other web frameworks utilize RESTful design principles in some form or another. By using RESTful principles, Flask apps are able to have a clear and standardized naming structure for routes and actions.


Example REST Workflow

For a real world case study, let us pretend that you have a newsletter application. The following is a high-level view of how such an app might work:

  1. You fill out the form on the 'New Newsletter' page and click submit.
  2. Data concerning you as the author, your newsletter content, and any additional information such as multimedia items is sent to the application server.
  3. The server interprets the information, recognizes that the request is for a new newsletter, generates the new record in the database, and performs background tasks (updating the newsletter counter, sending notification emails, etc).
  4. Next, the server sends a response back to the cilent. This does not necessarily mean that the newsletter was posted. The response could be that there was an error posting, or something along those lines. However, in this example we will say that the post went through properly, so the server sends a 201 response code and tells the browser which page to go to and render.
  5. Lastly, the browser receives the server information and shows you a message saying that your newsletter was successfully posted.

RESTful Conventions in Flask

Flask is lightweight, well-documented, and has a large community online (which gives you a wide population of people to provide help as you implement new tools in your Flask applications). Additionally, Flask provides native support for all HTTP request methods and makes it very easy to define routes- with these features, we can say that Flask natively supports REST conventions.

Let's take a look at how this would work for our newsletter application. RESTful routes fall into four familiar categories: Create, Read, Update, and Delete. These will roughly translate to five routes:

Category Action
Create Create the new newsletter instance.
Retrieve All (Index) Display a list of all newsletters.
Retrieve One Display an individual newsletter.
Update Update the newsletter instance.
Delete Delete an existing newsletter instance

HTTP Request Methods and RESTful Routes

To implement each of the above actions, we combine an HTTP request method (or HTTP verb) such as GET or POST with a route. Flask then maps each method/route combination to the appropriate view in the Flask application. The table below shows the HTTP request method and route we could use for this RESTful newsletter app:

HTTP Request Method Route Description
GET /newsletters Show all newsletters.
POST /newsletters Create a new newsletter.
GET /newsletters/<int:id> Show a specific newsletter.
PATCH or PUT /newsletters/<int:id> Update a specific newsletter.
DELETE /newsletters/<int:id> Delete a specific newsletter.

Note that even though we have five separate actions, we only have two routes defined in our application: /newsletters and /newsletters/<int:id>.

Flask does a great job of integrating RESTful routes into its system. If you can understand routes in Flask, you can understand REST in general.

How do RESTful routes benefit us as developers?

RESTful routes have a clear mapping between the URL resource and the corresponding backend actions.

Review of HTTP Request Methods

So what do GET, POST, et al. represent? These HTTP verbs give each HTTP request unique behavior. Below is an explanation of each verb:


Things to Keep in Mind throughout this Module

Below are a few keys to remember when thinking about REST:

Create

Retrieve All (Index)

Retrieve One

Update

Delete


Resources

Powered by Forestry.md