Async HTTP Requests in Python with aiohttp
An interactive command-line client that shows how to make asynchronous HTTP requests in Python with asyncio and aiohttp — sending GET, POST, PUT, and DELETE requests to a REST API and printing the JSON responses.
Python projects · HTTP and automation
Start here
Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in Async HTTP/Console_Code/. Install any third-party packages in an isolated Python environment.
Overview
This script demonstrates asynchronous HTTP communication by connecting to the Northwind sample REST API. It runs a loop that lets you repeatedly choose a request method, provide the necessary data, and view the JSON response — all using non-blocking I/O.
Features
- Supports GET, POST, PUT, and DELETE methods
- Asynchronous request handling via
aiohttpandasyncio - Interactive loop with an option to continue or exit after each request
- Pretty-printed JSON responses using
pprint - Targets the Northwind API’s
/categoriesresource for demonstration
Prerequisites and installation
aiohttp
Note:
asyncioandpprintare part of the Python standard library and do not require installation.
Install the external dependency:
python -m pip install aiohttp
Usage
From the Console_Code folder:
python HttpCall.py
The script uses http://northwind.now.sh/api/ as its base URL. Availability, redirects, and write behavior of that external sample service are not guaranteed by this repository. Use an API you control before trying POST, PUT, or DELETE; those methods can mutate shared remote data. The base URL ends with / and request paths start with /, so the constructed URLs also contain a double slash.
Example Session
Enter Method (GET, POST, PUT, DELETE): GET
# → prints list of categories
More! (Y|N): Y
Enter Method (GET, POST, PUT, DELETE): POST
Enter Category Id: 10
Enter Category Name: Beverages
Enter Category Description: Soft drinks and teas
# → prints created category
More! (Y|N): N
Supported Methods
| Method | Action | Input Required |
|---|---|---|
| GET | Retrieve all categories | None |
| POST | Create a new category | ID, Name, Description |
| PUT | Update an existing category | ID, Name, Description |
| DELETE | Delete a category | ID |
Architecture
Async HTTP/
└── Console_Code/
└── HttpCall.py # Main script
Troubleshooting and limitations
- Each request opens its own
aiohttp.ClientSession. That keeps the demo short; in larger programs, create one session and reuse it for many requests. - Every response is parsed as JSON without
raise_for_status()or a check for an empty body. HTML errors or a successful empty DELETE response can raise a JSON parsing error. The interactiveinput()calls block even though request I/O is asynchronous. - Entering any method other than GET, POST, PUT, or DELETE ends the loop.
Related documentation
Documentation links
Read all Python Projects and Code Examples guides online · Project overview · All developer projects