Skip to content

docs(event_handlers): note that CORS and */* binary mime type don't work in API Gateway #3383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ As a practical example, let's refactor our correlation ID middleware so it accep

These are native middlewares that may become native features depending on customer demand.

| Middleware | Purpose |
| ---------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| Middleware | Purpose |
| ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| [SchemaValidationMiddleware](/lambda/python/latest/api/event_handler/middlewares/schema_validation.html){target="_blank"} | Validates API request body and response against JSON Schema, using [Validation utility](../../utilities/validation.md){target="_blank"} |

#### Being a good citizen
Expand Down Expand Up @@ -648,15 +648,15 @@ You can compress with gzip and base64 encode your responses via `compress` param

### Binary responses

???+ warning "Amazon API Gateway does not support `*/*` binary media type [when CORS is also configured](https://github.com/aws-powertools/powertools-lambda-python/issues/3373#issuecomment-1821144779){target='blank'}."
This feature requires API Gateway to configure binary media types, see [our sample infrastructure](#required-resources) for reference.

For convenience, we automatically base64 encode binary responses. You can also use in combination with `compress` parameter if your client supports gzip.

Like `compress` feature, the client must send the `Accept` header with the correct media type.

???+ warning
This feature requires API Gateway to configure binary media types, see [our sample infrastructure](#required-resources) for reference.
!!! note "Lambda Function URLs handle binary media types automatically."

???+ note
Lambda Function URLs handle binary media types automatically.
=== "binary_responses.py"

```python hl_lines="17 23"
Expand Down
28 changes: 16 additions & 12 deletions examples/event_handler_rest/sam/micro_function_template.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
AWSTemplateFormatVersion: '2010-09-09'
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
micro-function-example

Globals:
Api:
TracingEnabled: true
Cors: # see CORS section
AllowOrigin: "'https://example.com'"
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
MaxAge: "'300'"
BinaryMediaTypes: # see Binary responses section
- "*~1*" # converts to */* for any binary type
Api:
TracingEnabled: true
Cors: # see CORS section
AllowOrigin: "'https://example.com'"
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
MaxAge: "'300'"
BinaryMediaTypes: # see Binary responses section
- "*~1*" # converts to */* for any binary type
# NOTE: use this stricter version if you're also using CORS; */* doesn't work with CORS
# see: https://github.com/aws-powertools/powertools-lambda-python/issues/3373#issuecomment-1821144779
# - "image~1*" # converts to image/*
# - "*~1csv" # converts to */csv, eg text/csv, application/csv

Function:
Timeout: 5
Runtime: python3.11
Function:
Timeout: 5
Runtime: python3.11

Resources:
# Lambda Function Solely For /users endpoint
Expand Down
7 changes: 6 additions & 1 deletion examples/event_handler_rest/sam/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ Globals:
MaxAge: "'300'"
BinaryMediaTypes: # see Binary responses section
- "*~1*" # converts to */* for any binary type
# NOTE: use this stricter version if you're also using CORS; */* doesn't work with CORS
# see: https://github.com/aws-powertools/powertools-lambda-python/issues/3373#issuecomment-1821144779
# - "image~1*" # converts to image/*
# - "*~1csv" # converts to */csv, eg text/csv, application/csv

Function:
Timeout: 5
Runtime: python3.9
Runtime: python3.11
Tracing: Active
Environment:
Variables:
Expand Down