Repository navigation
Replies: 2 comments 2 replies
|
import httpx
# In an ASGI handler or route:
transport = httpx.ASGITransport(app=downstream_asgi_app)
async with httpx.AsyncClient(transport=transport, base_url="http://internal") as client:
response = await client.get("/target-path")However, if your downstream services are independent HTTP microservices over a network, you need a reverse-proxy bridge. Rather than building custom request/response translation from scratch, the standard community tools for ASGI/Starlette reverse proxying with HTTPX are: 1. Dedicated proxy packages built on HTTPX
2. Manual forwarding with
|
|
One correction to the manual streaming example above: Use Starlette's from starlette.background import BackgroundTask
return StreamingResponse(
upstream_resp.aiter_raw(),
status_code=upstream_resp.status_code,
headers=resp_headers,
background=BackgroundTask(upstream_resp.aclose),
)This is the pattern in HTTPX's manual streaming documentation. I checked it with HTTPX 0.28.1 and Starlette 1.6.0 using an in-memory upstream and ASGI client: the proxy returned the streamed body, and the upstream response stream was closed afterward. The shared |
Uh oh!
There was an error while loading. Please reload this page.
I'm working through a POC for something and I'm investigating the possibility of using HTTPX as part of what effectively amounts to a reverse proxy. IE an ASGI / Starlette web app that has route which will recieve requests and forward them onto other microservives.
I wanted to know if httpx has anything that can aid transforming http requests from starlette (or raw ASGI) into httpx requests and visa versa with responses. I'm aware there could be quite a lot of complexity with this including chunked request / response bodies and http trailers to name just to edge cases.
Have I missed anything in httpx or supporting libraries that might help interpreting and forwarding ASGI requests?
All reactions