Crash Course in HTTP Requests Using Python

Getting your Python Code to Talk with the internet

Adam Snyder
May 28 · 4 min read
Photo by Markus Spiske on Unsplash

A lot of times, people struggle with how to take their programming knowledge and apply it to real-world scenarios. I’ve certainly been there and it can be frustrating. Writing simple programs that serve no real purpose other than to show an understanding of a concept is just not satisfying. We are creators. We want to develop something that has some sort of true purpose. Maybe you just learned Python and want to do something more with it, or you could use the data from an API to assist your other projects. Using the Python Requests module is a great way to accomplish these goals and help get your creative juices flowing for future possibilities.

The Internet

This could very well be a whole topic on its own, but for the purpose of the Requests module, we just need to understand the very basics. The internet, in a very basic sense, is just a bunch of computers linked together that can communicate with each other. When you type in a URL, such as, several things occur:


Application Programming Interface is a way for two applications to communicate with each other. Certain websites or web apps have an API that allows you to extract data from them for use in whatever you are creating. Not every website will have an API and although it’s still possible to extract data without the use of an API, that’s a topic more suited for web scraping. Some popular APIs include Google Maps, Facebook, Twitter, and Open Weather Map. Through the use of the Requests module and APIs, you’re able to retrieve that data and then create something with it.

Requests Module

The Requests module is an HTTP library that allows you to send requests and the ability to add headers, form data, and parameters with Python’s dictionaries. It will also receive JSON files and allow you to convert them into dictionaries. If you don’t already have PIP installed I suggest you download it. It’s a commonly used package installer for Python.

Once you have it installed this command in your terminal will download the Requests module:
python3 -m pip install requests

Import the module in your .py file:
import requests

To request information from the source and save it to the object “response” we use this get request:
response = requests.get(“”)

It’s a good idea to check the status code of the response with:

You can also check if the status code is ok and it will return a boolean with:

To access the data from the response we can use:

With the use of headers, we can specify, using a Python dictionary, a JSON file type instead of the HTML we got above with:
response = requests.get("", headers={"Accept": "application/json"})

We can then use a method to convert the JSON file into a Python dictionary:

When using an API, by going through the documentation you can find parameters which you can include in your get request to send more data about a particular request:
response = requests.get("", headers={"Accept": "application/json"}, params={"key1": "value1"})


This is a simple example I created when first learning to work with the Requests module. It connects to an API that provides dad jokes. This program asks the user for what type of joke they would like to hear, searches the API for that joke type and returns a joke or random joke (if more than one available).


The Request documentation will provide you with much more information on the module, while also being a valuable resource for if you get stuck. This crash course is meant to get your feet wet dealing with HTTP requests and give you an idea of what you can do with Python. Look up interesting APIs and see what kind of projects you can create with all this information at your fingertips. It’s a fun way to take your Python programming skills and interact with real-world data.

Better Programming

Advice for programmers.

Adam Snyder

Written by

Student programmer with big dreams. Hoping to build up the people around me.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade