This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: REST API

Serving a REST API

face Josiah Wang

In the next chapter, you will try to send HTTP requests to a REST API.

There are some toy REST API that you can use for experimental purposes.

But I thought that it will be more fun to actually run a simple REST API server on your own computer, and communicate with it via HTTP requests. It will help you understand better what goes on on both sides.

I have written a simple toy blog API using Flask. There is no need to understand the code (this is not a Flask tutorial). You just need to get it running so that we can experiment with making requests to the server. The code for the API will be pretty much self-explanatory though once you start making requests and understand what is going on.

Clone my repo to anywhere on your computer.

cd to the project repo.

Set up an environment with its required dependencies to run the Web API server.

  • create a virtual environment by running python3 -m venv venv
  • activate the environment by running source venv/bin/activate
  • install the dependencies by executing pip install --upgrade pip && pip install -r requirements.txt

Then run the server by executing python3 app.py.

Go to http://localhost:5000 in your browser. It should display a JSON object:

{
  "message": "Welcome to the demo blog API!"
}

You can also try http://localhost:5000/articles to retrieve all articles from the blog API. You should get 10 articles.