You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import random
from fastapi import APIRouter, Response
from fastapi.middleware.gzip import GZipMiddleware
router = APIRouter(tags=['public'])
app = create_app()
app.add_middleware(GZipMiddleware, minimum_size=1000)
# AWS Lambda support
handler = Mangum(app, lifespan="off")
@router.get("/update/{current_state}/download")
async def download_something_binary() -> Response:
# Create some random bytes
import random
open('/tmp/data', 'wb').write(random.randbytes(1024 * 100))
# Return the binary payload
f = open('/tmp/data', 'rb')
return Response(
status_code=200,
content=f.read(), headers={
"Content-Type": "application/octet-stream",
"Content-Disposition": "attachment; filename=data.tar.gz"})
I have also tried returning just a dict.
All the time I get the data out on the client as base64 encoded data.
As I understand Mangum should automatically figure out if the Content-Type is not one of the text_mime_types that it should set the isBase64Encoded for API Gateway to understand to convert it to binary at the client side.
Have I misunderstood something or am I doing something wrong here? please help.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi there
Mangum documentation states that binary responses are by default: https://mangum.io/http/#configuring-binary-responses
But I simply can't get it to work, can someone tell me what I'm doing wrong?
I have also tried returning just a dict.
All the time I get the data out on the client as base64 encoded data.
As I understand Mangum should automatically figure out if the Content-Type is not one of the text_mime_types that it should set the isBase64Encoded for API Gateway to understand to convert it to binary at the client side.
Have I misunderstood something or am I doing something wrong here? please help.
Beta Was this translation helpful? Give feedback.
All reactions