GET request with StackOverflow API, Postman, and Python

Yash Tewani
4 min readNov 14, 2023

--

For anyone like myself who does not know the practical implementations of API, this article will be a brief introduction to RestAPI, as well as its methods. We will use one of those methods to obtain data from the Question forum on a StackOverflow API using ‘indexing’ in Python, and we will test the output of our Python script with Postman.

What is Rest API?

RESTful API is like a common language for applications hosted on the internet to communicate with one another. It follows a set of rules, making it easy for different systems to share information smoothly. REST API is considered stateful, meaning each request from a client to a server contains all the information needed to understand and process that request. The server doesn’t store any information about the client between requests, and each request is independent. In simpler terms, it’s the web’s behind-the-scenes tool that makes everything work together seamlessly.

Rest API follows four different HTTP Methods:

  • GET: Retrieves data from a specified resource, like loading a webpage.
  • POST: Submits data to be processed, often used for creating new resources or submitting forms.
  • PUT: Updates a resource or creates a new one if it doesn’t exist, indicating a desire to modify or replace the current state
  • DELETE: Requests the removal of a resource, signaling the intention to delete a specific item or account.

For the sake of this article, we will only experiment with the ‘GET’ method. So without further ado, let’s get it. Get. Hah.

Prerequisites: Python Environment, Postman, Curiosity for RestAPI

First, head over to the Stack Exchange API for Questions. At the bottom of the page, we can observe part of a web address that represents the API request for all questions and answers on the StackOverflow page.

You can hit run to observe the JSON Format, but we will continue with Postman

With Postman open, and a fresh dashboard created, we will copy only the domain name of the webpage, [https://api.stackexchange.com] and in addition, copy the ‘request’ portion we previously observed, and paste it into your Postman API URL space. Ensure to select ‘GET” as our method.

Hit Send!

You can observe the API request in its JSON structure; we can identify the key: value structure in the script. You may also note that there is a key ‘is_answered’ along with some StackOverflow links as references for answers to specific questions. Note that each question in this API is listed within an ‘item’. So to query the questions, we must query the ‘item’.

Let us create a simple Python application using indexing, that will parse through all questions. For questions that are answered, we will declare ‘answered’. For unanswered questions, let us fetch that link.

To interact with our RestAPI web service through Python, we need to install ‘requests’. Open your terminal, & install ‘requests’ package using pip.

pip3 install requests 

That is all the work we have to do with terminal. I will explain the program into three simple blocks and will show you the resulting output.

import requests # Library for communicating with web servivces & RESTful APIs
import json # module provides tools for working with JSON data

The variable ‘response’ holds the result of the HTTP GET request made to the Stack Exchange API. It contains information returned by the API, such as the latest questions from Stack Overflow, in a format that can be further processed and analyzed. Paste the link from Postman here.

response=requests.get('http://api.stackexchange.com/2.3/questions?order=desc&sort=activity&site=stackoverflow')

In Python, we can use indexing to extract only the information we desire, using only the ‘key’ pair. That is the beauty of JSON. We will use an ‘if-else’ statement along with boolean values to parse through the API. If ‘is_answered’ is false, resulting in an unanswered question, we will print the title of that question along with its respective link. Else, print “question answered”.

for data in response.json()['items']: #declaring data as our variable to iterate through
if data['is_answered'] == False: #If the question is unanswered
print(data['title']) #looking for key pair 'title'
print(data['link']) #Looking for key pair 'link'
print() # This is to add a space
else: #If the question is answered
print("Question Answered")
print() # This is to add a space
Be sure to double-check your tab spacing!

Below is the output of our simple, yet effective Python program. We are able to use a GET request to surf through the entire API request, and only index out our desired key values, and you can use Postman to observe and test the API request once you have your desired output.

Voila, nice and neat.

One small step for API’s. Thank you for tuning in!

--

--

Yash Tewani

Graduated with A.S. and B.S. in Computer Science and Information Security. Interests are rooted in problem solving related to Business and Technology