Comments (3)
@jrobbins-LiveData I can provide some general information, but would need more details to say much more. The decoding is done by the maybe_encode_body function based on the value of isBase64Encoded
from the event for a handler like this. If I'm not misunderstanding, then sounds like you may want to use a custom handler subclass to override this, but just taking my best guess atm.
from mangum.
Here's a more concrete version of my question.
First, here's where the base64 feature works perfectly. Using pypdf, for example, I can merge some PDF files, in memory, and simply return a FastAPI Response
of the bytes, and the ALB adapter of Mangum does exactly what I want.
# some FastAPI route that receives a JSON body giving instructions about what PDF files to merge
...
output_io = io.BytesIO()
merger = PdfMerger()
for pdf_io in pdf_ios:
merger.append(pdf_io)
merger.write(output_io)
merger.close()
return Response(output_io.getvalue(), media_type='application/pdf')
In a different route, I need to return a screenshot from a Selenium embedded browser using this api. If I get the screenshot as bytes, then the code is equivalent to the PDF example:
...
browser.get(url)
screenshot = browser.get_screenshot_as_png()
return Response(content=screenshot, media_type='image/png')
But, suppose for performance reasons, I need to call get_screenshot_as_base64 instead of get_screenshot_as_png?
...
browser.get(url)
screenshot = browser.get_screenshot_as_base64()
# NOTE this currently does not work because `maybe_encode_body` base64-encodes the already encoded string
return Response(content=screenshot, media_type='image/png')
The ALB body
@Property uses the incoming request event as its flag, but, since a REST API taking JSON as its input might need to return a base64-encoded response, it would be nice to somehow be able to flag this without reference to the incoming event.
@property
def body(self) -> bytes:
return maybe_encode_body(
self.event.get("body", b""),
is_base64=self.event.get("isBase64Encoded", False),
)
How would I use the handler subclass technique you referred to in order to solve this?
from mangum.
It's impossible to handle right now, as Response
expects bytes
and is irreversibly mangled by handle_base64_response_body
function.
from mangum.
Related Issues (20)
- Basic Auth on FastAPI+API Gateway HOT 2
- Support for S3 Events HOT 3
- AWS lambda function not identify the handler in FastAPI project
- FYI: documentation is not available at mangum.io HOT 4
- Question: How does one keep state between AWS Lambda cold starts? HOT 5
- How To: How to form SQS event to look like REST body as if coming from API Gateway HOT 1
- Support for streaming? HOT 3
- Problems with Sanic 23.3.0 and Mangum 0.17.0 HOT 5
- Wrong domain when using AWS Lambda FunctionUrls and CloudFront with FastAPI
- Handle comma separated X-Forwarded-port
- SQS message not returned back to the topic HOT 1
- FastAPI redirects to wrong host with Mangum HOT 2
- Doesn't work with Lambda in Docker locally HOT 2
- Mangum breaks Django POST with multipart/form-data; HOT 2
- `OSError` in Vercel Serverless Functions deployment
- Official support for Python 3.11 and Python 3.12? HOT 1
- `Request.url` does not include the api_gateway_base_path
- VPC Lattice event support
- Dealing with long running client HOT 4
- mangum.io Domain Expired HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mangum.