Search code examples
pythonfastapiuvicornhttpx

fastapi/uvicorn prevent ungzipping with httpx.AsyncClinet


i work on reverse proxy based on fastapi. I want transparenty send data requested by AsyncClient. I have problem with gziped pages. Please can you help me, how to prevent default ungzipping of resp.content on this example?

@app.get("/{path:path}")
async def _get ( path: str, request: Request ):
    url = await my_proxy_logic (path, request)
    async with httpx.AsyncClient() as client:
        req = client.build_request("GET", url)
        resp = await client.send(req, stream=False)
    return Response( status_code=resp.status_code, headers=resp.headers, content=resp.content)```

Solution

  • It is possible to extract undecoded data from httpx response only in case of streaming mode stream=True or httpx.stream. In the example below, I collect the entire response using aiter_raw and return it from the path operation. Keep in mind that the entire response is loaded into memory, if you want to avoid this use fastapi StreamingResponse

    import httpx
    from fastapi import FastAPI, Request, Response
    
    app = FastAPI()
    
    
    @app.get("/pass")
    async def root(request: Request):
        async with httpx.AsyncClient() as client:
            req = client.build_request('GET', 'http://httpbin.org/gzip')
            resp = await client.send(req, stream=True)
            return Response(status_code=resp.status_code, headers=resp.headers,
                            content=b"".join([part async for part in resp.aiter_raw()]))