Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

842
Visualizações
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

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda