Skip to content

Commit a164291

Browse files
committed
fix: cors default behaviour; sample req/rep
1 parent 48b43fd commit a164291

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -247,27 +247,27 @@ def __init__(self, proxy_type: Enum = ProxyEventType.APIGatewayProxyEvent, cors:
247247
self._cors = cors
248248
self._cors_methods: Set[str] = {"OPTIONS"}
249249

250-
def get(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
250+
def get(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
251251
"""Get route decorator with GET `method`"""
252252
return self.route(rule, "GET", cors, compress, cache_control)
253253

254-
def post(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
254+
def post(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
255255
"""Post route decorator with POST `method`"""
256256
return self.route(rule, "POST", cors, compress, cache_control)
257257

258-
def put(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
258+
def put(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
259259
"""Put route decorator with PUT `method`"""
260260
return self.route(rule, "PUT", cors, compress, cache_control)
261261

262-
def delete(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
262+
def delete(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
263263
"""Delete route decorator with DELETE `method`"""
264264
return self.route(rule, "DELETE", cors, compress, cache_control)
265265

266-
def patch(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
266+
def patch(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
267267
"""Patch route decorator with PATCH `method`"""
268268
return self.route(rule, "PATCH", cors, compress, cache_control)
269269

270-
def route(self, rule: str, method: str, cors: bool = False, compress: bool = False, cache_control: str = None):
270+
def route(self, rule: str, method: str, cors: bool = True, compress: bool = False, cache_control: str = None):
271271
"""Route decorator includes parameter `method`"""
272272

273273
def register_resolver(func: Callable):

docs/core/event_handler/api_gateway.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This is the sample infrastructure we are using for the initial examples in this
3939
AllowOrigin: "'https://example.com'"
4040
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date'"
4141
MaxAge: "'300'"
42-
BinaryMediaTypes: # see Binary responses
42+
BinaryMediaTypes: # see Binary responses section
4343
- '*~1*' # converts to */* for any binary type
4444
Function:
4545
Timeout: 5
@@ -186,6 +186,19 @@ Here's an example where we have two separate functions to resolve two paths: `/h
186186
}
187187
```
188188

189+
=== "response.json"
190+
191+
```json
192+
{
193+
"statusCode": 200,
194+
"headers": {
195+
"Content-Type": "application/json"
196+
},
197+
"body": "{\"message\":\"hello universe\"}",
198+
"isBase64Encoded": false
199+
}
200+
```
201+
189202
#### HTTP API
190203

191204
When using API Gateway HTTP API to front your Lambda functions, you can instruct `ApiGatewayResolver` to conform with their contract via `proxy_type` param:
@@ -267,6 +280,17 @@ You can use `/path/{dynamic_value}` when configuring dynamic URL paths. This all
267280
return app.resolve(event, context)
268281
```
269282

283+
=== "sample_request.json"
284+
285+
```json
286+
{
287+
"resource": "/hello/{name}",
288+
"path": "/hello/lessa",
289+
"httpMethod": "GET",
290+
...
291+
}
292+
```
293+
270294
You can also nest paths as configured earlier in [our sample infrastructure](#required-resources): `/{message}/{name}`.
271295

272296
=== "app.py"
@@ -292,6 +316,17 @@ You can also nest paths as configured earlier in [our sample infrastructure](#re
292316
return app.resolve(event, context)
293317
```
294318

319+
=== "sample_request.json"
320+
321+
```json
322+
{
323+
"resource": "/{message}/{name}",
324+
"path": "/hi/michael",
325+
"httpMethod": "GET",
326+
...
327+
}
328+
```
329+
295330
### Accessing request details
296331

297332
By integrating with [Data classes utilities](../../utilities/data_classes.md){target="_blank"}, you have access to request details, Lambda context and also some convenient methods.
@@ -384,6 +419,35 @@ This will ensure that CORS headers are always returned as part of the response w
384419
return app.resolve(event, context)
385420
```
386421

422+
=== "response.json"
423+
424+
```json
425+
{
426+
"statusCode": 200,
427+
"headers": {
428+
"Content-Type": "application/json",
429+
"Access-Control-Allow-Origin": "https://www.example.com",
430+
"Access-Control-Allow-Headers": "Authorization,Content-Type,X-Amz-Date,X-Amz-Security-Token,X-Api-Key"
431+
},
432+
"body": "{\"message\":\"hello lessa\"}",
433+
"isBase64Encoded": false
434+
}
435+
```
436+
437+
=== "response_no_cors.json"
438+
439+
```json
440+
{
441+
"statusCode": 200,
442+
"headers": {
443+
"Content-Type": "application/json"
444+
},
445+
"body": "{\"message\":\"hello lessa\"}",
446+
"isBase64Encoded": false
447+
}
448+
```
449+
450+
387451
!!! tip "Optionally disable class on a per path basis with `cors=False` parameter"
388452

389453
#### Pre-flight

0 commit comments

Comments
 (0)