From 61b9909ac3925d51eb767542b70c82e57905b392 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Thu, 20 Oct 2022 11:42:55 -0400 Subject: [PATCH] fix(HTTP Request Node): avoid error when response doesn't include content-type (#4365) * :bug: Fix bug when response doesn't include content type * :zap: Improve autodetect response format * :zap: Make content-type match more specific * :zap: Improve list of content-types to download --- .../nodes/HttpRequest/GenericFunctions.ts | 26 +++++++++++++++++++ .../HttpRequest/V3/HttpRequestV3.node.ts | 10 ++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts b/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts index 7b4a9edd14a87..d055f72840b67 100644 --- a/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts +++ b/packages/nodes-base/nodes/HttpRequest/GenericFunctions.ts @@ -68,3 +68,29 @@ export const getOAuth2AdditionalParameters = (nodeCredentialType: string) => { }; return oAuth2Options[nodeCredentialType]; }; + +//https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types +export const binaryContentTypes = [ + 'image/', + 'audio/', + 'video/', + 'application/octet-stream', + 'application/gzip', + 'application/zip', + 'application/vnd.rar', + 'application/epub+zip', + 'application/x-bzip', + 'application/x-bzip2', + 'application/x-cdf', + 'application/vnd.amazon.ebook', + 'application/msword', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'application/vnd.ms-fontobject', + 'application/vnd.oasis.opendocument.presentation', + 'application/pdf', + 'application/x-tar', + 'application/vnd.visio', + 'application/vnd.ms-excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/x-7z-compressed', +]; diff --git a/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts b/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts index e87183993af9f..a305250c94969 100644 --- a/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts +++ b/packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts @@ -12,7 +12,11 @@ import { import { OptionsWithUri } from 'request-promise-native'; -import { getOAuth2AdditionalParameters, replaceNullValues } from '../GenericFunctions'; +import { + binaryContentTypes, + getOAuth2AdditionalParameters, + replaceNullValues, +} from '../GenericFunctions'; export class HttpRequestV3 implements INodeType { description: INodeTypeDescription; @@ -1204,11 +1208,11 @@ export class HttpRequestV3 implements INodeType { ) as boolean; if (autoDetectResponseFormat) { - const responseContentType = response.headers['content-type']; + const responseContentType = response.headers['content-type'] ?? ''; if (responseContentType.includes('application/json')) { responseFormat = 'json'; response.body = JSON.parse(Buffer.from(response.body).toString()); - } else if (['image', 'audio', 'video'].some((e) => responseContentType.includes(e))) { + } else if (binaryContentTypes.some((e) => responseContentType.includes(e))) { responseFormat = 'file'; } else { responseFormat = 'text';