Processing JSON Requests with aiohttp in Python

Mar 3, 2024 ยท 3 min read

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 request.json() method. This will parse the request body and return a Python dict with the JSON data:

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 data variable will contain that dict data after parsing.

Handling Missing or Invalid JSON

If the request doesn't have a valid JSON body, request.json() will raise an exception. You can handle this by wrapping it in a try/except:

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 pydantic:

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:

  • Use request.json() to parse JSON body data
  • Wrap in try/except to catch JSON parsing errors
  • Validate data with JSON schema libraries like pydantic
  • Access JSON fields directly on the parsed dict data
  • Return 4xx responses for invalid requests
  • 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 request.json(), JSON schemas, and proper validation and error handling, you can robustly handle JSON request bodies. Pay special attention to potential errors and invalid data cases to make your services more resilient.

    Browse by tags:

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you


    Try ProxiesAPI for free

    curl "http://api.proxiesapi.com/?key=API_KEY&url=https://example.com"

    <!doctype html>
    <html>
    <head>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    ...

    X

    Don't leave just yet!

    Enter your email below to claim your free API key: