Skip to content Skip to sidebar Skip to footer

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.

Solution 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/")asyncdefapi_data(request: Request):
    params = str(request.query_params)
    url = f'http://some.other.api/{params}'
    response = RedirectResponse(url=url)
    return response

Solution 2:

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

 @app.get("/")defread_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

Post a Comment for "Fastapi Variable Query Parameters"