{ Server Side HTTP Requests. }


By the end of this chapter, you should be able to:

  • Use the requests module to perform server side requests
  • Compare and contrast client and server side API requests


In this section, we will be using terms like HTTP, APIs, AJAX and headers. If you are not familiar with these terms, take a look at the section on How The Web Works in our Intermediate JavaScript Part II course.

Using Python, we can make make HTTP requests to send and retrieve data from APIs. Although we can do this with JavaScript as well (using a technology called AJAX), using JavaScript will not always work because of security reasons.

Instead, we can make requests from one server to another server (and if you are comfortable with JavaScript, you can send back a response to the browser from "our" server). Here is what that might look like.

To issue server side requests we use the requests module, which you can install using pip3 install requests. You can read more about it here.

Let's start with a very simple example to get data from the OMDB API.

import requests

# Make an HTTP GET request to a specific URL
r = requests.get('http://omdbapi.com?t=titanic') # returns a response object
r.status_code # 200
r.ok # True
r.headers # see a dictionary of HTTP headers
r.json() # Examine what this data looks like in JSON

We can also use the requests module to make a POST request:

import requests

payload = dict(id=1)

r = requests.post('some_url', params=payload)

r.text # see your params in an args dictionary

The requests module is very useful for sending and retrieving data from an API and can also be used to authenticate users (logging in through Facebook / Twitter / etc.).

When you're ready, move on to Web Scraping Exercises


Creative Commons License