HomeContact
Python
Simple GET with python requests
Karthikeya
February 27, 2021
1 min

GET is the most simplest of all HTTP method. The GET method as the name suggests is used to fetch information from a server. More resource on GET call here. In this post we will learn how to make a GET call with python and its requests library.

PRE-REQUISITES

  • python 3.x (installation guide)
  • requests library (install requests)

For the sake of simplicity we will avoid APIs with authentication feature. This is discussed in another post here.

We will be using GET API hosted by jsonplaceholder. The API of interest for us is /posts which will return a random placeholder list of posts object.

1. SIMPLE GET CALL:

import requests
from pprint import pprint

url = "https://jsonplaceholder.typicode.com/posts/"
response = requests.get(url)
pprint(response.json())

NOTE : pprint is just used to pretty print the response. normal print function would also return the same value but without any formatting applied.

  • First we start with importing the requests library.
  • The variable url holds the complete path for the GET operation.
  • We use the get() method provided by requests library to make a GET call and store the response returned by the operation in variable response.
  • response.json() returns the response of the GET call as a list of dictionary similar to sample response.

python console output will be something similar to this.

[
    {
        'body': 'quia et suscipit\n'
        'suscipit recusandae consequuntur expedita et cum\n'
        'reprehenderit molestiae ut ut quas totam\n'
        'nostrum rerum est autem sunt rem eveniet architecto',
        'id': 1,
        'title': 'sunt aut facere repellat provident occaecati excepturi optio '
        'reprehenderit',
        'userId': 1
    },
    {
        'body': 'est rerum tempore vitae\n'
        'sequi sint nihil reprehenderit dolor beatae ea dolores neque\n'
        'fugiat blanditiis voluptate porro vel nihil molestiae ut '
        'reiciendis\n'
        'qui aperiam non debitis possimus qui neque nisi nulla',
        'id': 20,
        'title': 'qui est esse',
        'userId': 2
    },
    ...
]

2. FILTERING RESULTS WITH QUERY PARAMETERS:

API call from previous call will return all the posts in the database. Now, let’s say you are just interested in posts from a particular user id. You will have to go through the entire response to filter out posts from a user id. That’s where query parameters come in. They provide a way for you to query for only the results you are interested in.

QUERY PARAM IN URL::

https://jsonplaceholder.typicode.com/posts?userId=1
  • userId is a query parameter, and 1 is the userId value we are interested in.
  • query params are prepended by ? indicating the start of query.
  • query parameter will have a key and a value (key = userID and value = 1)
  • If there are multiple query parameters, they are seperated by &

SAMPLE QUERY WITH MULTIPLE PARAMETERS::

?userId=1&title='abc'

QUERY PARAM IN URL:

import requests
from pprint import pprint

url = "https://jsonplaceholder.typicode.com/posts?userId=1"
response = requests.get(url)
pprint(response.json())

OUTPUT:

[
  {
        'body': 'quia et suscipit\n'
        'suscipit recusandae consequuntur expedita et cum\n'
        'reprehenderit molestiae ut ut quas totam\n'
        'nostrum rerum est autem sunt rem eveniet architecto',
        'id': 1,
        'title': 'sunt aut facere repellat provident occaecati excepturi optio '
        'reprehenderit',
        'userId': 1
    },
    {
        'body': 'est rerum tempore vitae\n'
        'sequi sint nihil reprehenderit dolor beatae ea dolores neque\n'
        'fugiat blanditiis voluptate porro vel nihil molestiae ut '
        'reiciendis\n'
        'qui aperiam non debitis possimus qui neque nisi nulla',
        'id': 2,
        'title': 'qui est esse',
        'userId': 1
    },
    ...
]

PARAMETRS AS DICTIONARY::

import requests

url = "https://jsonplaceholder.typicode.com/posts"
params = { "userId" : 1 }
response = requests.get(url, params=params)
print(response.url)
print(response.json())

OUTPUT:

https://jsonplaceholder.typicode.com/posts?userId=1
[
  {
        'body': 'quia et suscipit\n'
        'suscipit recusandae consequuntur expedita et cum\n'
        'reprehenderit molestiae ut ut quas totam\n'
        'nostrum rerum est autem sunt rem eveniet architecto',
        'id': 1,
        'title': 'sunt aut facere repellat provident occaecati excepturi optio '
        'reprehenderit',
        'userId': 1
    },
    {
        'body': 'est rerum tempore vitae\n'
        'sequi sint nihil reprehenderit dolor beatae ea dolores neque\n'
        'fugiat blanditiis voluptate porro vel nihil molestiae ut '
        'reiciendis\n'
        'qui aperiam non debitis possimus qui neque nisi nulla',
        'id': 2,
        'title': 'qui est esse',
        'userId': 1
    },
    ...
]

Related Posts

Deep comparison of JSON in python
November 18, 2021
1 min
© 2021, All Rights Reserved.

Quick Links

About UsContact UsSite Map

Social Media