fastapi-proxy-lib

raw JSON →
0.3.0 verified Mon Apr 27 auth: no python

HTTP/WebSocket proxy middleware for Starlette/FastAPI, version 0.3.0, allowing you to add a reverse proxy endpoint with minimal code. Released as needed.

pip install fastapi-proxy-lib
error TypeError: reverse_proxy() missing 1 required positional argument: 'url'
cause Old usage passing a request object instead of a URL string.
fix
Call reverse_proxy('https://example.com/path') with a string URL.
error httpx.InvalidURL: Invalid URL: '/api'
cause Relative URL passed to reverse_proxy.
fix
Provide an absolute URL including scheme and host, e.g., 'https://api.example.com/api'.
breaking In 0.3.0, the signature of `reverse_proxy` changed; the first argument is now a URL string instead of a request object. Old code will raise TypeError.
fix Update to `await reverse_proxy('https://target.com/path')` instead of `await reverse_proxy(request, url='https://target.com/path')`.
gotcha The `reverse_proxy` function expects an absolute URL. Relative paths will cause an httpx.InvalidURL exception.
fix Always provide a fully qualified URL (including scheme and host) to `reverse_proxy`.
deprecated The `add_route_proxy` function was deprecated in 0.3.0 in favor of `reverse_proxy`. It will be removed in a future version.
fix Replace `add_route_proxy(app, ...)` with a custom route that calls `reverse_proxy`.

Creates a FastAPI app with a catch-all proxy endpoint that forwards requests to httpbin.

from fastapi import FastAPI
from fastapi_proxy_lib import reverse_proxy

app = FastAPI()

@app.get('/proxy/{path:path}')
async def proxy(path: str):
    target = f'https://httpbin.org/{path}'
    return await reverse_proxy(target)

# run with: uvicorn main:app --reload