Sling Academy
Home/Python/Using aiohttp to send GET requests in Python (2 examples)

Using aiohttp to send GET requests in Python (2 examples)

Last updated: August 16, 2023

aiohttp is an open-source library that provides an asynchronous HTTP client and server. It is based on the asyncio module and supports both HTTP/1.1 and HTTP/2 protocols. This easy-to-understand article will walk you through the steps to use aiohttp and the async/await syntax to efficiently send GET requests in Python.

Making GET requests with aiohttp

Below are the steps:

1. aiohttp is not built-in, so you need to install it first:

pip install aiohttp

2. Next, you need to import the aiohttp and asyncio modules in your Python script. These modules provide the functionality for creating an asynchronous HTTP client and running it in an event loop:

import aiohttp
import asyncio

3. In this step, you need to define an async function that will contain the logic for making the GET request. This function will use the async with statement to create and manage a session object, which represents a connection to the server:

async def main(url):
    async with aiohttp.ClientSession() as session:
        # we'll make GET request here
        pass

4. Inside the async with block, you need to use the session.get() method to send a GET request to a URL of your choice. This method returns a response object, which you can also access with the async with statement. In this example, we’ll use the following API endpoint for practice:

https://api.slingacademy.com

Here’s the code:

import aiohttp
import asyncio

url = "https://api.slingacademy.com"

async def main(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            # do something with the response here
            pass

5. Inside the second async with block, you can use various attributes and methods of the response object to access the data returned by the server. For example, you can use response.status to get the status code, response.headers to get the headers, response.text() to get the text content, response.json() to get the JSON content, etc. You need to use the await keyword before calling these methods, as they are also asynchronous.

For instance, you can update the main() function like this:

async def main(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            print(f"Status: {response.status}")
            print(f"Content-type: {response.headers['content-type']}")
            print(f"Data: {await response.json()}")
            pass

You’ll still see nothing happens because we haven’t executed the main() function yet. Let’s do this in the final step below.

6. You need to create an event loop object and run your async function in it. This will execute your code asynchronously and handle any errors or exceptions that may occur. The simplest way is to call asyncio.run():

# Run the main function
asyncio.run(main(url))

Complete code:

# SlingAcademy.com
# This code uses Python 3.11.4

import aiohttp
import asyncio

url = "https://api.slingacademy.com/"

async def main(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            print(f"Status: {response.status}")
            print(f"Content-type: {response.headers['content-type']}")
            print(f"Data: {await response.json()}")
            pass

# Run the main function
asyncio.run(main(url))

Output:

Status: 200
Content-type: application/json
Data: {'success': True, 'message': 'Welcome to Sling Academy Public API'}

Setting params and headers

In order to set params and headers when sending GET requests with aiohttp, you can pass them as arguments to the session.get() method. For instance, if you want to send a GET request to this URL:

https://api.slingacademy.com/v1/sample-data/users

with the params offset=5 and limit=3 and the header Content-type: application/json, you can do as shown below:

# SlingAcademy.com
# This code uses Python 3.11.4

import aiohttp
import asyncio

api_url = "https://api.slingacademy.com/v1/sample-data/users"


async def main():
    async with aiohttp.ClientSession() as session:
        params = {"offset": 5, "limit": 3}
        headers = {"Content-type": "application/json"}
        async with session.get(api_url, params=params, headers=headers) as response:
            print(f"Status: {response.status}")
            print(f"Data: {await response.json()}")
            pass

asyncio.run(main())

Output:

Status: 200
Data: {'success': True, 'time': '2023-08-16 00:22:18 UTC', 'message': 'Sample data for testing and learning purposes', 'total_users': 1000, 'offset': 5, 'limit': 3, 'users': [{'gender': 'male', 'id': 6, 'date_of_birth': '1902-08-06T00:00:00', 'job': 'Financial manager', 'city': 'Amymouth', 'zipcode': '52484', 'latitude': -78.303175, 'profile_picture': 'https://api.slingacademy.com/public/sample-users/6.png', 'first_name': 'Joshua', 'last_name': 'Delgado', 'email': '[email protected]', 'phone': '216-424-8988x0838', 'street': '1663 Simmons Points', 'state': 'Colorado', 'country': 'Angola', 'longitude': -141.625538}, {'gender': 'male', 'id': 7, 'date_of_birth': '1911-01-21T00:00:00', 'job': 'Child psychotherapist', 'city': 'New Valerieton', 'zipcode': '84185', 'latitude': -71.2706315, 'profile_picture': 'https://api.slingacademy.com/public/sample-users/7.png', 'first_name': 'Kyle', 'last_name': 'Brown', 'email': '[email protected]', 'phone': '+1-602-863-6880x1291', 'street': '203 Taylor Place Apt. 725', 'state': 'West Virginia', 'country': 'Burundi', 'longitude': -11.632148}, {'gender': 'female', 'id': 8, 'date_of_birth': '1952-09-23T00:00:00', 'job': 'Aeronautical engineer', 'city': 'Dustinstad', 'zipcode': '45577', 'latitude': 63.089701, 'profile_picture': 'https://api.slingacademy.com/public/sample-users/8.png', 'first_name': 'Janice', 'last_name': 'Ramirez', 'email': '[email protected]', 'phone': '001-353-958-2737x873', 'street': '02762 Thomas Viaduct', 'state': 'South Carolina', 'country': 'Burkina Faso', 'longitude': -84.763775}]}

You can find more details about the API used in the example above on this page: Sample Users – Free Fake API for Practicing & Prototyping.

Next Article: Using aiohttp to make POST requests in Python (with examples)

Previous Article: Fixing Python Requests SSLError: CERTIFICATE_VERIFY_FAILED

Series: Python: Network & JSON tutorials

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots