[Python] How to forward FastAPI requests to another server?

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have a FastAPI application for testing/development purposes. What I want is that any request that arrives to my main app to automatically be sent, as is, to a different app on another server, with exactly the same parameters and same endpoint. This is not a redirect, because I still want the main app to process requests and return values as usual. I just want to initiate a similar request to a different version of the app on a different server, without waiting for the answer from that server, so that the second app gets the request as if the original request was sent to it.

    How can I achieve that? Below is a sample code that I use for handling the request:

    @app.post("/my_endpoint/some_parameters")
    def process_request(
    params: MyParamsClass,
    pwd: str = Depends(authenticate),
    ):
    # send the same request to http://my_other_url/my_endpoint/
    return_value = process_the_request(params)
    return return_value.as_json()

    Continue reading...

Compartilhe esta Página