From e3f0ee68b47378d86f24c6943b30d226a9fe9ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Mon, 15 Aug 2022 10:42:00 +0200 Subject: [PATCH] [Fix #3540] Handle iterator request bodies in `convertN8nRequestToAxios` (#3567) * Fix issue with request body not being sent when using the new request helper --- packages/core/src/NodeExecuteFunctions.ts | 24 ++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 11240779d0..0171dd1849 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -677,6 +677,10 @@ async function proxyRequestToAxios( }); } +function isIterator(obj: unknown): boolean { + return obj instanceof Object && Symbol.iterator in obj; +} + function convertN8nRequestToAxios(n8nRequest: IHttpRequestOptions): AxiosRequestConfig { // Destructure properties with the same name first. const { headers, method, timeout, auth, proxy, url } = n8nRequest; @@ -716,21 +720,17 @@ function convertN8nRequestToAxios(n8nRequest: IHttpRequestOptions): AxiosRequest }; } - // if there is a body and it's empty (does not have properties), - // make sure not to send anything in it as some services fail when - // sending GET request with empty body. - if (n8nRequest.body && Object.keys(n8nRequest.body).length) { - axiosRequest.data = n8nRequest.body; + const { body } = n8nRequest; + if (body) { // Let's add some useful header standards here. const existingContentTypeHeaderKey = searchForHeader(axiosRequest.headers, 'content-type'); if (existingContentTypeHeaderKey === undefined) { + axiosRequest.headers = axiosRequest.headers || {}; // We are only setting content type headers if the user did // not set it already manually. We're not overriding, even if it's wrong. - if (axiosRequest.data instanceof FormData) { - axiosRequest.headers = axiosRequest.headers || {}; + if (body instanceof FormData) { axiosRequest.headers['Content-Type'] = 'multipart/form-data'; - } else if (axiosRequest.data instanceof URLSearchParams) { - axiosRequest.headers = axiosRequest.headers || {}; + } else if (body instanceof URLSearchParams) { axiosRequest.headers['Content-Type'] = 'application/x-www-form-urlencoded'; } } else if ( @@ -738,6 +738,12 @@ function convertN8nRequestToAxios(n8nRequest: IHttpRequestOptions): AxiosRequest ) { axiosRequest.data = new URLSearchParams(n8nRequest.body as Record); } + // if there is a body and it's empty (does not have properties), + // make sure not to send anything in it as some services fail when + // sending GET request with empty body. + if (isIterator(body) || Object.keys(body).length > 0) { + axiosRequest.data = body; + } } if (n8nRequest.json) {