from fastapi import FastAPI from fastapi.middleware.gzip import GZipMiddleware from mangum import Mangum app = FastAPI() app.add_middleware(GZipMiddleware, minimum_size=1000) @app.get("/") async def main(): return "somebigcontent" handler = Mangum(app, TEXT_MIME_TYPES=["application/vnd.some.type"])
Configuring binary responses
Binary responses are determined using the
Content-Encoding headers from the event request and a list of text MIME types.
Text MIME types
By default, all response data will be base64 encoded and include
isBase64Encoded=True in the response except the default text MIME types and any MIME types included in the
TEXT_MIME_TYPES list setting.
The following types are excluded from binary responses by default:
Content-Type header prefixed with
text/ is automatically excluded.
Content-Encoding header is set to
br, then a binary response will be returned regardless of MIME type.
HTTPCycle is used by the adapter to communicate message events between the application and AWS. It is a state machine that handles the entire ASGI request and response cycle.
mangum.protocols.http.HTTPCycle(scope, text_mime_types, state=
Manages the application cycle for an ASGI
- scope - A dictionary containing the connection scope used to run the ASGI application instance.
- text_mime_types - A list of mime types of MIME types that should not return a binary response in API Gateway.
- state - An enumerated
HTTPCycleStatetype that indicates the state of the ASGI connection.
- app_queue - An asyncio queue (FIFO) containing messages to be received by the application.
- response - A dictionary containing the response data to return in AWS Lambda.
Calls the application with the
http connection scope.
Awaited by the application to receive ASGI
Awaited by the application to send ASGI
mangum.protocols.http.HTTPCycleState(value, names=None, *, module=None, qualname=None, type=None, start=1)
The state of the ASGI
- REQUEST - Initial state. The ASGI application instance will be run with the
connection scope containing the
- RESPONSE - The
http.response.startevent has been sent by the application. The next expected message is the
http.response.bodyevent, containing the body content. An application may pass the
more_bodyargument to send content in chunks, however content will always be returned in a single response, never streamed.
- COMPLETE - The body content from the ASGI application has been completely read. A disconnect event will be sent to the application, and the response will be returned.