Making HTTP calls: My Wholesome Guide to Learning Python (Part 5)

Nepal Brothers
3 min readSep 29, 2021

Knowing how to make HTTP calls or any other external calls is a staple thing in software development. You would typically make a call to some sort of service and then based on the response, you decide what you want to do.

There are many ways to do it on python, and one of the ways to do it is by using a library called requests

What does our request look like?

GET /posts HTTP/1.1
Host: jsonplaceholder.typicode.com

So, we are going to make a HTTP GET call in the jsonplaceholder.typicode.com and get the response.

See it yourself when you click here https://jsonplaceholder.typicode.com/posts

Make sure you are in virtual environment of the project

You may already have a virtual environment, but if you haven’t here is how you would create and use one.

Be sure to check the blog below if you are looking to learn more about virtual environment.

Install requests library

Once you are in a virtual environment, you would want to use

pip install requests

Make a request

This seems very easy. Line 6 will make a request and what is returned from it is saved in the response variable. You can use it as dictionary or as text, based on what you would like.

So, that is the base knowledge of how you would want to make a HTTP call. But, now lets make it look better, build on top of it, and learn some interactions that usually occur while developing on python.

Server resource endpoints

These are the endpoints, and lets make our code interact elegantly with them. This is taken from https://jsonplaceholder.typicode.com/

Step 1: Do it manually and see what response looks like

You can use curl, postman or browser itself to see what the response looks like for the GET request.

If you look at the response, you can see it has userId , id , title and body . So, it would be a good practice to create a dataclass for it.

Test the BlogPost class manually

Before making a request, make sure you test if your dataclass resolves to the value you have observed in the response. For eg, for this response, we want to make sure we construct the BlogPost class.

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat",
"body": "quia et suscipitnsuscipit"
}

If you are little confused on line 25 about why we are using 2 asterisks (2 stars) sign there, be sure to check out this post, but the main point is that the json keys are mapped to the BlogPost’s fields.

GET request and convert a list to data class

Here, we make a request in get_blog_posts() . Initially, line 17 creates an empty list, and once we get the proper response, we go through each one of the items in the response we received. One by one, we convert them into dataclass.

Make the code look shorter and prettier

There is one optimization that we can make that is more python-y and will our code short. We are going to use single line for loop, which basically is going to convert the response into a list of BlogPost.

Pass query parameters in get request

We are going to use the params parameter to add any query parameters into the request. We are going to use this request

GET /comments?postId=1

Get request and convert a single json object

POST a new BlogPost into the server

Update existing blog post

--

--