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.
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.
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.
requests
library. url
holds the complete path for the GET operation.get()
method provided by requests library to make a GET call
and store the response returned by the operation in variable 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 }, ... ]
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
?
indicating the start of query.&
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 }, ... ]
Quick Links
Legal Information
Social Media