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

Chapter 4: HTTP request methods

GET request

face Josiah Wang

The four most commonly used HTTP request methods are POST, GET, PUT, and DELETE. There are also a few others which we will not discuss.

You have used the GET request method so far. The ‘action’ associated with a GET request is usually to read or retrieve something. This is usually the default HTTP method if not specified.

Let’s start by trying to retrieve all articles on our blog API using a GET request. You can also check out the live log shown on your server terminal to see the request that you have just made!

>>> import requests
>>> response = requests.get("http://localhost:5000/articles")
>>> print(response.status_code)
200
>>> print(response.headers["Content-Type"])
application/json
>>> print(response.json())
[{'author': 1, 'content': "Look at the time, you've got less than 4 minutes, please hurry. Maybe you were adopted. Are those my clocks I hear? So what's it to you,  ...

As you can see, you have now retrieved a JSON object instead of an HTML source code. You can then do whatever you want with this data. For example, you might want to display the articles in your app.

In a REST API, what you have retrieved is called a resource. Think of it as an object instance (containing the various HTTP methods).

Our blog API also allows you to retrieve a specific article. Let’s retrieve the article with ID 3.

>>> response = requests.get("http://localhost:5000/articles/3")
>>> print(response.json())
{'author': 2, 'content': 'Ronald Reagon. Our first television set, Dad just
picked it up today. Do you have a television? What a nightmare. Excuse me.
Wow, you must be rich.', 'id': 3, 'title': 'What did you say?'}