9951 explained code solutions for 126 technologies


python-aiohttpHow to make a JSON request with aiohttp in Python?


Making a JSON request with aiohttp in Python is easy and straightforward. Here is an example code block to make a JSON request:

import aiohttp

async with aiohttp.ClientSession() as session:
    async with session.get('https://example.com/api/endpoint') as resp:
        json_data = await resp.json()

The output of the code block above will be a JSON object.

Code explanation

  1. import aiohttp - imports the aiohttp library
  2. async with aiohttp.ClientSession() as session - creates a client session
  3. async with session.get('https://example.com/api/endpoint') as resp - makes a GET request to the specified endpoint
  4. json_data = await resp.json() - stores the response as a JSON object

Helpful links

Edit this code on GitHub