From 7e4abf74b882c4d44158767d69fb37fa9086d8e6 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Wed, 9 Mar 2022 09:01:41 -0800 Subject: [PATCH] chore(middleware-content-length): set transfer-encoding if content-length is not set (#3403) --- packages/middleware-content-length/src/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/middleware-content-length/src/index.ts b/packages/middleware-content-length/src/index.ts index 79fd958c0901..a6a2e8b1c6f1 100644 --- a/packages/middleware-content-length/src/index.ts +++ b/packages/middleware-content-length/src/index.ts @@ -11,6 +11,7 @@ import { } from "@aws-sdk/types"; const CONTENT_LENGTH_HEADER = "content-length"; +const TRANSFER_ENCODING_HEADER = "transfer-encoding"; export function contentLengthMiddleware(bodyLengthChecker: BodyLengthCalculator): BuildMiddleware { return (next: BuildHandler): BuildHandler => @@ -24,12 +25,17 @@ export function contentLengthMiddleware(bodyLengthChecker: BodyLengthCalculator) .map((str) => str.toLowerCase()) .indexOf(CONTENT_LENGTH_HEADER) === -1 ) { - const length = bodyLengthChecker(body); - if (length !== undefined) { + try { + const length = bodyLengthChecker(body); request.headers = { ...request.headers, [CONTENT_LENGTH_HEADER]: String(length), }; + } catch (error) { + request.headers = { + ...request.headers, + [TRANSFER_ENCODING_HEADER]: "chunked", + }; } } }