diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 29b0c140e7..7fcf724401 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -135,6 +135,28 @@ function searchForHeader(headers: IDataObject, headerName: string) { return headerNames.find((thisHeader) => thisHeader.toLowerCase() === headerName); } +async function generateContentLengthHeader(formData: FormData, headers: IDataObject) { + if (!formData || !formData.getLength) { + return; + } + try { + const length = await new Promise((res, rej) => { + formData.getLength((error: Error | null, length: number) => { + if (error) { + rej(error); + return; + } + res(length); + }); + }); + headers = Object.assign(headers, { + 'content-length': length, + }); + } catch (error) { + Logger.error('Unable to calculate form data length', { error }); + } +} + async function parseRequestObject(requestObject: IDataObject) { // This function is a temporary implementation // That translates all http requests done via @@ -199,6 +221,7 @@ async function parseRequestObject(requestObject: IDataObject) { delete axiosConfig.headers[contentTypeHeaderKeyName]; const headers = axiosConfig.data.getHeaders(); axiosConfig.headers = Object.assign(axiosConfig.headers || {}, headers); + await generateContentLengthHeader(axiosConfig.data, axiosConfig.headers); } else { // When using the `form` property it means the content should be x-www-form-urlencoded. if (requestObject.form !== undefined && requestObject.body === undefined) { @@ -235,6 +258,7 @@ async function parseRequestObject(requestObject: IDataObject) { // Mix in headers as FormData creates the boundary. const headers = axiosConfig.data.getHeaders(); axiosConfig.headers = Object.assign(axiosConfig.headers || {}, headers); + await generateContentLengthHeader(axiosConfig.data, axiosConfig.headers); } else if (requestObject.body !== undefined) { // If we have body and possibly form if (requestObject.form !== undefined) {