Skip to content

Commit 48b43fd

Browse files
committed
docs: add fine grained response section
1 parent 19c9dbd commit 48b43fd

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

docs/core/event_handler/api_gateway.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,43 @@ Key | Value | Note
408408

409409
### Fine grained responses
410410

411+
You can use the `Response` class to have full control over the response, for example you might want to add additional headers or set a custom Content-type.
412+
413+
=== "app.py"
414+
415+
```python hl_lines="10-14"
416+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver, Response
417+
418+
app = ApiGatewayResolver()
419+
420+
@app.get("/hello")
421+
def get_hello_you():
422+
payload = json.dumps({"message": "I'm a teapot"})
423+
custom_headers = {"X-Custom": "X-Value"}
424+
425+
return Response(status_code=418,
426+
content_type="application/json",
427+
body=payload,
428+
headers=custom_headers
429+
)
430+
431+
def lambda_handler(event, context):
432+
return app.resolve(event, context)
433+
```
434+
435+
=== "response.json"
436+
437+
```json
438+
{
439+
"body": "{\"message\":\"I\'m a teapot\"}",
440+
"headers": {
441+
"Content-Type": "application/json",
442+
"X-Custom": "X-Value"
443+
},
444+
"isBase64Encoded": false,
445+
"statusCode": 418
446+
}
447+
411448
### Compress
412449

413450
You can compress with gzip and base64 encode your responses via `compress` parameter.
@@ -417,7 +454,7 @@ You can compress with gzip and base64 encode your responses via `compress` param
417454
=== "app.py"
418455

419456
```python hl_lines="5 7"
420-
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver, Response
457+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
421458

422459
app = ApiGatewayResolver()
423460

0 commit comments

Comments
 (0)