Replies: 22 comments 16 replies
-
I have the same issue! Did you find a solution? |
Beta Was this translation helpful? Give feedback.
-
This is not quite working but it might be a start?
|
Beta Was this translation helpful? Give feedback.
-
How would I allow any path to be resolved by the proxy ? With flask you can do something like this
Can I allow wildcard paths with fastAPI ? |
Beta Was this translation helpful? Give feedback.
-
Any change that anyone made progress with this? I'm still looking for a solution |
Beta Was this translation helpful? Give feedback.
-
@eelkeh @thomas-maschler I just ran into this myself and found it was covered in the docs here referencing accessing a file system. A proxy should resolve the path same way.
so my proxy use case will look like:
|
Beta Was this translation helpful? Give feedback.
-
For larger file and smaller memory footprint you can stream the file like this as described in httpx docs here: client = AsyncClient(base_url=f'http://containername:7800/')
@app.get(“/tiles/{path:path}”)
async def tile_request(path: str):
req = client.build_request("GET", path)
r = await client.send(req, stream=True)
return StreamingResponse(
r.aiter_raw(),
background=BackgroundTask(r.aclose),
headers=r.headers
) |
Beta Was this translation helpful? Give feedback.
-
I found this thread, and I thought I'd add my complete example in case someone else stumbles across it: from starlette.requests import Request
from starlette.responses import StreamingResponse
from starlette.background import BackgroundTask
import httpx
client = httpx.AsyncClient(base_url="http://containername:7800/")
async def _reverse_proxy(request: Request):
url = httpx.URL(path=request.url.path,
query=request.url.query.encode("utf-8"))
rp_req = client.build_request(request.method, url,
headers=request.headers.raw,
content=await request.body())
rp_resp = await client.send(rp_req, stream=True)
return StreamingResponse(
rp_resp.aiter_raw(),
status_code=rp_resp.status_code,
headers=rp_resp.headers,
background=BackgroundTask(rp_resp.aclose),
)
app.add_route("/titles/{path:path}",
_reverse_proxy, ["GET", "POST"]) |
Beta Was this translation helpful? Give feedback.
-
Hello @aebrahim and others, what if the request we want to proxy is streaming a lot of data as input? The solution by @aebrahim waits for all the request input to arrive first, and then will end up sending it all at once to the target server, am I right? If yes, do you have any idea on how to update the solution to better cover this case as well? Thank you very much!! Also, any reason for using only |
Beta Was this translation helpful? Give feedback.
-
Also, in my opinion, this issue should be reopened and considered a feature request. |
Beta Was this translation helpful? Give feedback.
-
@aebrahim Have you tried to make it work also with |
Beta Was this translation helpful? Give feedback.
-
Update: I was incorrect. |
Beta Was this translation helpful? Give feedback.
-
@JarroVGIT are you sure? I think you're confusing input with output. I agree that their solution correctly streams the output of the target server (e.g. for downloading a large file from the target server). That's where the |
Beta Was this translation helpful? Give feedback.
-
My bad, sorry. |
Beta Was this translation helpful? Give feedback.
-
@papb I think streaming requests on httpx doc is what you are looking for. Meaning just remove the |
Beta Was this translation helpful? Give feedback.
-
@JarroVGIT @morty29 oh cool, thanks, I will try that. |
Beta Was this translation helpful? Give feedback.
-
Whelp, you are right, my apologies! |
Beta Was this translation helpful? Give feedback.
-
@papb does the large request come in with uploaded files or with just a huge request body? |
Beta Was this translation helpful? Give feedback.
-
@JarroVGIT No problem :) Thank you for all the attention so far.
I don't have a specific request in mind, so please assume that both cases may happen. My goal is to understand how to make the best possible reverse-proxy-like API handler in FastAPI. The solution by @aebrahim covered all cases I could think of except for requests with nontrivial inputs (such as a huge request body), so that's why I asked about that. By the way I saw you recommending just using a real reverse proxy in a comment for another issue, and for my use case I am currently doing just that (using mitmproxy), but I found it cumbersome to have something else running along FastAPI (since I need more things than just a reverse-proxy). I feel that the suggestion by @morty29 probably works, I just haven't had the time to try it out yet. |
Beta Was this translation helpful? Give feedback.
-
To pass incoming request streaming data just pass
|
Beta Was this translation helpful? Give feedback.
-
Is there any straightforward way to also reverse proxy any websocket routes, any static files, any resource, any route, through a particular base route for another server? For example, |
Beta Was this translation helpful? Give feedback.
-
I am using the reverse_proxy function from [this comment] to proxy video urls in my fastapi app. |
Beta Was this translation helpful? Give feedback.
-
Hi, every one here. I encountered this issue a month ago. Then, I developed https://github.com/WSH032/fastapi-proxy-lib for it. I implemented I also implemented Hope it's helpful for newcomers to this. from fastapi_proxy_lib.fastapi.app import reverse_http_app
app = reverse_http_app(base_url="http://www.example.com/foo/") |
Beta Was this translation helpful? Give feedback.
-
This may be a silly question. Can fastapi proxy another site as a response to the request?
I did some research. All pages I found are Nginx reverse proxy fastapi. I already did that to solve the SSL problem.
My system workflow:
RedirectResponse is a rewrite, not a proxy.
Static Files are for local files.
HTMLResponse is for static HTML.
Can you give me some tips on how to proxy another site in fastapi? Thank you.
Beta Was this translation helpful? Give feedback.
All reactions