Introduction to HTTP Requests in Python and Flask

Introduction to HTTP Requests in Python and Flask

HTTP Requests are a series of methods for "requesting" data etc. from a given resource. Previously we have used requests within our Flask applications for requesting resources at URL's on our server. In those situations we used "GET" requests within our app and we recieved HTML templates from the server.

We will now begin using HTTP requests to request data from external resources, known as API's or Application Programming Interface. In the following example we will request data from a Pokemon REST (Representational State Transfer) API. We will input URL's into our GET requests and recieve JSON data from the external server.

Let's test with in browser first

Before working with Python let's make sure we understand the basics of requests with a no code solution. We can use the Pokemon API (https://pokeapi.co/) to learn about requests.On the site we are given a root URL https://pokeapi.co/api/v2/ that makes up the beginning string of our URLs. We can then add in pokemon/charmander to get a JSON response back from the server. We can even paste this into the browser URL.

The Response

{
    "abilities": [
        {
            "ability": {
                "name": "blaze",
                "url": "https://pokeapi.co/api/v2/ability/66/"
            },
            "is_hidden": false,
            "slot": 1
        },
        {
            "ability": {
                "name": "solar-power",
                "url": "https://pokeapi.co/api/v2/ability/94/"
            },
            "is_hidden": true,
            "slot": 3
        }
    ],
}

After pasting into the browser, using on the site, or testing in Postman (discussed in class) we get a JSON response back. NOTE: I did not show all the data given in this article as it would take up a lot of space. We can see in browser we are given a lot of data. This is why we practiced working with dictionaries (objects) and lists (arrays) so much earlier.

Requests in Python

We need to import the JSON and requests packages from the Python standard library before we can get started.

import requests
import json

Next we can define our request with r or request and initiate a GET request

r = requests.get("https://pokeapi.co/api/v2/pokemon/charmander").json()
print(r)

We should see the same JSON response in the Python console we saw in the browser. We can now take this data and use it in our Flask application.

Requests in Flask

data = requests.get("https://pokeapi.co/api/v2/pokemon?limit=150&offset=0").json()

Let's start by creating a data variable and initiating a new get request. This request will return a JSON object that has a results property that contains the first 150 Pokemon (The best ones). Let's pass that data into a template

@app.route("/")
    def index():
        return render_template('index.html', data=data)

In our template for Index we can do a simple for loop and display the Pokemon names. Since the monster names are in the results list we need to specify data.results rather than simply writing data. If this is unclear test the get request in browser or on the site and ensure you understand the JSON response.

 {% for monster in data.results %}
   <h3>{{monster.name}}</h3>
    {% endfor %}

You should now have an HTML response with the first 150 pokemon names showing up!