When building web APIs and services with Python's aiohttp library, you'll often need to handle JSON data sent in requests. aiohttp makes it simple to get JSON data from POST requests and use it in your handlers. In this guide, we'll look at some practical examples for processing JSON request data with aiohttp.
Getting the Request POST Body as JSON
To access the request body data as JSON, use the
import aiohttp
from aiohttp import web
async def handle(request):
data = await request.json()
print(data)
return web.Response(text="OK")
app = web.Application()
app.add_routes([web.post('/', handle)])
So if a client sends a POST request with this body:
{
"name": "John",
"age": 30
}
The
Handling Missing or Invalid JSON
If the request doesn't have a valid JSON body,
try:
data = await request.json()
except aiohttp.ContentTypeError:
return web.Response(text="Invalid JSON", status=400)
This will return a 400 response if the JSON is malformed or missing.
Accessing JSON Fields in the Handler
Once you have the JSON data as a Python dict, accessing fields is easy:
name = data['name']
age = data['age']
You can then use these values in your route handler code.
JSON Schema Validation
To validate JSON data against a schema, use a library like
from pydantic import BaseModel, ValidationError
class User(BaseModel):
name: str
age: int
try:
user = User(**data)
except ValidationError as e:
return web.Response(text=str(e), status=422)
This will ensure the JSON data matches your model before using it further.
Tips for Processing JSON with aiohttp
Here are some useful tips when handling JSON:
Example: POST JSON Handler
Here is an aiohttp handler example that puts some of these techniques together:
from aiohttp import web
import pydantic
class User(pydantic.BaseModel):
name: str
age: int
async def handle_post(request):
try:
data = await request.json()
user = User(**data)
except (aiohttp.ContentTypeError, pydantic.ValidationError):
return web.Response(status=400, text="Invalid JSON data")
print(f"Created user {user.name} ({user.age})")
return web.Response(status=201, text="Created user")
app = web.Application()
app.add_routes([web.post('/users', handle_post)])
This shows JSON validation and handling in a real aiohttp route.
Processing JSON data from requests is very common in aiohttp services. Using