You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/event-handler/rest.md
+59-8Lines changed: 59 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -514,33 +514,84 @@ You can enable response compression by using the `compress` middleware. This wil
514
514
515
515
### Binary responses
516
516
517
-
!!! note "Coming soon"
517
+
If you need to return binary data, there are several ways you can do so based on how much control you require.
518
+
519
+
#### Auto serialization
518
520
519
521
As described in the [response auto serialization](#response-auto-serialization) section, when you return a JavaScript object from your route handler, we automatically serialize it to JSON and set the `Content-Type` header to `application/json`.
520
522
521
-
If you need to return binary data (e.g. images, PDFs, etc), you will need to return an API Gateway Proxy result directly, setting the `isBase64Encoded` flag to `true` and base64 encoding the binary data, as well as setting the appropriate `Content-Type` header.
523
+
A similar pattern applies to binary data where you can return an `ArrayBuffer`,
524
+
a [Nodejs stream](https://nodejs.org/api/stream.html){target="_blank"}, or
525
+
a [Web stream](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API#browser_compatibility){target="_blank"}
526
+
directly from your handler. We will automatically serialize the response by setting the `isBase64Encoded` flag to `true` and `base64` encoding the binary data.
527
+
528
+
!!! note "Content types"
529
+
The default header will be set to `application/json`. If you wish to change this,
530
+
e.g., in the case of images, PDFs, videos, etc, then you should use the `reqCtx.res.headers` object to set the appropriate header.
You can use binary responses together with the [`compress`](#compress) feature, and the client must send the `Accept` header with the correct media type.
550
+
#### Set `isBase64Encoded` parameter
542
551
543
-
We plan to add first-class support for binary responses in a future release. Please [check this issue](https://github.com/aws-powertools/powertools-lambda-typescript/issues/4514) for more details and examples, and add 👍 if you would like us to prioritize it.
552
+
You can indicate that you wish to `base64` encode any response, regardless of type, by setting the `isBase64Encoded` field in `reqCtx` to `true`.
If you wish to use binary responses together with the [`compress`](#compress) feature, the client must send the `Accept` header with the correct media type.
0 commit comments