Skip to content

Instantly share code, notes, and snippets.

@imankulov
Last active Jun 8, 2022
Embed
What would you like to do?
Parse JSON-encoded parameters from request queries with FastAPI

Problem: FastAPI doesn't accept JSON-encoded pydantic models in query strings. See #884.

Solution: Use json_param() from the snippet below.

Usage example.

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class User(BaseModel):
    name: str

@app.get("/")
def root(user: User = json_param("user", User, description="User object")):
    return {"message": f"Hello, {user!r}"}

Request and response examples (with httpie)

Success:

$ http localhost:7000 user=='{"name": "Foo"}'
HTTP/1.1 200 OK

{
    "message": "Hello, User(name='Foo')"
}

Validation error:

HTTP/1.1 400 Bad Request

{
    "detail": [
        {
            "loc": [
                "name"
            ],
            "msg": "none is not an allowed value",
            "type": "type_error.none.not_allowed"
        }
    ]
}
from typing import Any
import pydantic
from fastapi import Depends, HTTPException, Query
from pydantic import Json, ValidationError
def json_param(param_name: str, model: Any, **query_kwargs):
"""Parse JSON-encoded query parameters as pydantic models.
The function returns a `Depends()` instance that takes the JSON-encoded value from
the query parameter `param_name` and converts it to a Pydantic model, defined
by the `model` attribute.
"""
def get_parsed_object(value: Json = Query(alias=param_name, **query_kwargs)):
try:
return pydantic.parse_obj_as(model, value)
except ValidationError as err:
raise HTTPException(400, detail=err.errors())
return Depends(get_parsed_object)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment