'FastAPI variable query parameters

I am writing a Fast API server that accepts requests, checks if users are authorized and then redirects them to another URL if successful.

I need to carry over URL parameters, e.g.

http://localhost:80/data/?param1=val1&param2=val2 should redirect to http://some.other.api/?param1=val1&param2=val2, thus keeping previously allotted parameters.

The parameters are not controlled by me and could change at any moment.

How can I achieve this?

Code:

from fastapi import FastAPI
from starlette.responses import RedirectResponse

app = FastAPI()

@app.get("/data/")
async def api_data():
    params = '' # I need this value
    url = f'http://some.other.api/{params}'
    response = RedirectResponse(url=url)
    return response



Solution 1:[1]

In the docs they talk about using the Request directly, which then lead me to this:

from fastapi import FastAPI, Request
from starlette.responses import RedirectResponse

app = FastAPI()

@app.get("/data/")
async def api_data(request: Request):
    params = request.query_params
    url = f'http://some.other.api/?{params}'
    response = RedirectResponse(url=url)
    return response

Solution 2:[2]

As mention in docs of FastAPI https://fastapi.tiangolo.com/tutorial/query-params-str-validations/.

 @app.get("/")
 def read_root(param1: Optional[str] = None, param2: Optional[str] = None):
     url = f'http://some.other.api/{param1}/{param2}'
     return {'url': str(url)}

output

enter image description here

enter image description here

Solution 3:[3]

If the query parameters are known when starting the API but you still wish to have them dynamically set:

from fastapi import FastAPI, Depends
from pydantic import create_model

app = FastAPI()

# Put your query arguments in this dict
query_params = {"name": (str, "me")}

query_model = create_model("Query", **params) # This is subclass of pydantic BaseModel

# Create a route
@app.get("/items")
async def get_items(params: query_model = Depends()):
    params_as_dict = params.dict()
    ...

This has the benefit that you see the parameters in the automatic documentation:

Swagger UI

But you are still able to define them dynamically (when starting the API).

Note: if your model has dicts, lists or other BaseModels as field types, the request body pops up. GET should not have body content so you might want to avoid those types.

See more about dynamic model creation from Pydantic documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LeoRochael
Solution 2 qaiser
Solution 3 miksus