2

I'm using the following schemas for my FastAPI Get request:

class JWTClaims(BaseModel):
    iat: int
    exp: int
    aud: Optional[Union[str, List[str]]]
    iss: str
    sub: str
    azp: str


class AccessClaims(JWTClaims):
    scope: str


@router.get("/client")
async def get_user_client(claims:AccessClaims = Depends()):
    pass

FastAPI is trying to convert the aud field as part of a request body- how can I eliminate this so that it becomes a valid GET request?

enter image description here

6
  • try: aud: Optional[Union[str, List[str]]] = None Commented Sep 7, 2022 at 14:04
  • That doesn't get rid of the request body. I can delete the contents of the request body and it works however this aud field will be populated at various points so I can't use this as a solution Commented Sep 7, 2022 at 14:08
  • one more idea.. aud: Optional[Union[str, List[str]]] = Query(default=None), of course you need from fastapi import Query Commented Sep 7, 2022 at 14:17
  • 1
    As noted in Chris link this isn't currently possible - the limitation is documented in the reference guide: "To declare a query parameter with a type of list, like in the example above, you need to explicitly use Query, otherwise it would be interpreted as a request body." Commented Sep 7, 2022 at 20:29
  • 1
    thanks @Chris, that does clarify things a lot Commented Sep 8, 2022 at 8:18

1 Answer 1

0

Here a workaround, you can add the aud as argument of the route like this:

class JWTClaims(BaseModel):
    iat: int
    exp: int
    iss: str
    sub: str
    azp: str


class AccessClaims(JWTClaims):
    scope: str


app = FastAPI()


# as argument it works
@app.get("/client")
async def get_user_client(aud: Optional[List[str]] = Query(None), claims:AccessClaims = Depends()):
    return

And you have the aud in the swagger. It will be a list but, users can supply only one if they want.

The swagger with aud

For reason I don't understand, doing this in the class doesn't work:

class JWTClaims(BaseModel):
    iat: int
    exp: int
    aud: Optional[List[str]] = Query(None)
    iss: str
    sub: str
    azp: str

If you're really want the Optional[str, List[str]], you will need to have it as a body request:

@app.get("/client")
async def get_user_client(aud: Optional[Union[str, List[str]]] = Query(None), claims:AccessClaims = Depends()):
    return

leads to this error when loading the app:

    async def get_user_client(aud: Optional[Union[str, List[str]]] = Query(None), claims:AccessClaims = Depends()):
  File "/home/stackoverflow/lib/python3.10/site-packages/fastapi/routing.py", line 626, in decorator
    self.add_api_route(
  File "/home/stackoverflow/lib/python3.10/site-packages/fastapi/routing.py", line 565, in add_api_route
    route = route_class(
  File "/home/stackoverflow/lib/python3.10/site-packages/fastapi/routing.py", line 434, in __init__
    self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
  File "/home/stackoverflow/lib/python3.10/site-packages/fastapi/dependencies/utils.py", line 322, in get_dependant
    assert isinstance(
AssertionError: Param: aud can only be a request body, using Body()

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.