Skip to content

Encode binary HTTP responses automatically - #2355

Open
mnapoli wants to merge 1 commit into
4.xfrom
auto-binary-responses
Open

mnapoli wants to merge 1 commit into
4.xfrom
auto-binary-responses

Conversation

@mnapoli

@mnapoli mnapoli commented Dec 2, 2026

Copy link
Copy Markdown
Member

Returning an image, a PDF or any non-UTF-8 body without BREF_BINARY_RESPONSES fails with a 500 (HTTP API) or a 502 (REST API, Function URL, ALB): buffered responses are JSON, which only holds UTF-8.

Bref now encodes the body in base64 when it is not valid UTF-8 (or contains NUL bytes, which ALB truncates). Text responses are unchanged, so there is nothing to configure and no infrastructure change: HTTP APIs, Function URLs and ALBs decode isBase64Encoded natively. REST APIs still need binaryMediaTypes, as before. Streamed responses (Function URLs in RESPONSE_STREAM mode) already sent bytes as is.

BREF_BINARY_RESPONSES=1 still encodes every response.

Validated on Lambda with the v4 runtime: PNG, random bytes and Latin-1 text arrive intact through HTTP API, REST API (with binaryMediaTypes), buffered Function URL, ALB, FPM and PSR-15. The UTF-8 check costs ~0.4 ms per MB, less than the JSON encoding of the same body.

Buffered responses are JSON, which only holds UTF-8: a binary body made the
invocation fail (a 500 or 502) unless BREF_BINARY_RESPONSES was set.

This branch was successfully deployed

1 active deployment
preview — 6718d699 Deployed Dec 2, 2026 by mnapoli via deploy #171
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant