Skip to content

Commit 19f5641

Browse files
authored
docs(event_handlers): note that CORS and */* binary mime type don't work in API Gateway (#3383)
1 parent 8e0ab8c commit 19f5641

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

docs/core/event_handler/api_gateway.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,8 @@ As a practical example, let's refactor our correlation ID middleware so it accep
573573

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

576-
| Middleware | Purpose |
577-
| ---------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
576+
| Middleware | Purpose |
577+
| ------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
578578
| [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"} |
579579

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

649649
### Binary responses
650650

651+
???+ 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'}."
652+
This feature requires API Gateway to configure binary media types, see [our sample infrastructure](#required-resources) for reference.
653+
651654
For convenience, we automatically base64 encode binary responses. You can also use in combination with `compress` parameter if your client supports gzip.
652655

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

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

658-
???+ note
659-
Lambda Function URLs handle binary media types automatically.
660660
=== "binary_responses.py"
661661

662662
```python hl_lines="17 23"

examples/event_handler_rest/sam/micro_function_template.yaml

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1-
AWSTemplateFormatVersion: '2010-09-09'
1+
AWSTemplateFormatVersion: "2010-09-09"
22
Transform: AWS::Serverless-2016-10-31
33
Description: >
44
micro-function-example
55
66
Globals:
7-
Api:
8-
TracingEnabled: true
9-
Cors: # see CORS section
10-
AllowOrigin: "'https://example.com'"
11-
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
12-
MaxAge: "'300'"
13-
BinaryMediaTypes: # see Binary responses section
14-
- "*~1*" # converts to */* for any binary type
7+
Api:
8+
TracingEnabled: true
9+
Cors: # see CORS section
10+
AllowOrigin: "'https://example.com'"
11+
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
12+
MaxAge: "'300'"
13+
BinaryMediaTypes: # see Binary responses section
14+
- "*~1*" # converts to */* for any binary type
15+
# NOTE: use this stricter version if you're also using CORS; */* doesn't work with CORS
16+
# see: https://github.com/aws-powertools/powertools-lambda-python/issues/3373#issuecomment-1821144779
17+
# - "image~1*" # converts to image/*
18+
# - "*~1csv" # converts to */csv, eg text/csv, application/csv
1519

16-
Function:
17-
Timeout: 5
18-
Runtime: python3.11
20+
Function:
21+
Timeout: 5
22+
Runtime: python3.11
1923

2024
Resources:
2125
# Lambda Function Solely For /users endpoint

examples/event_handler_rest/sam/template.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ Globals:
1111
MaxAge: "'300'"
1212
BinaryMediaTypes: # see Binary responses section
1313
- "*~1*" # converts to */* for any binary type
14+
# NOTE: use this stricter version if you're also using CORS; */* doesn't work with CORS
15+
# see: https://github.com/aws-powertools/powertools-lambda-python/issues/3373#issuecomment-1821144779
16+
# - "image~1*" # converts to image/*
17+
# - "*~1csv" # converts to */csv, eg text/csv, application/csv
18+
1419
Function:
1520
Timeout: 5
16-
Runtime: python3.9
21+
Runtime: python3.11
1722
Tracing: Active
1823
Environment:
1924
Variables:

0 commit comments

Comments
 (0)