mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(HTTP Request Node): avoid error when response doesn't include content-type (#4365)
* 🐛 Fix bug when response doesn't include content type * ⚡ Improve autodetect response format * ⚡ Make content-type match more specific * ⚡ Improve list of content-types to download
This commit is contained in:
parent
7fcd821cad
commit
61b9909ac3
|
@ -68,3 +68,29 @@ export const getOAuth2AdditionalParameters = (nodeCredentialType: string) => {
|
||||||
};
|
};
|
||||||
return oAuth2Options[nodeCredentialType];
|
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',
|
||||||
|
];
|
||||||
|
|
|
@ -12,7 +12,11 @@ import {
|
||||||
|
|
||||||
import { OptionsWithUri } from 'request-promise-native';
|
import { OptionsWithUri } from 'request-promise-native';
|
||||||
|
|
||||||
import { getOAuth2AdditionalParameters, replaceNullValues } from '../GenericFunctions';
|
import {
|
||||||
|
binaryContentTypes,
|
||||||
|
getOAuth2AdditionalParameters,
|
||||||
|
replaceNullValues,
|
||||||
|
} from '../GenericFunctions';
|
||||||
export class HttpRequestV3 implements INodeType {
|
export class HttpRequestV3 implements INodeType {
|
||||||
description: INodeTypeDescription;
|
description: INodeTypeDescription;
|
||||||
|
|
||||||
|
@ -1204,11 +1208,11 @@ export class HttpRequestV3 implements INodeType {
|
||||||
) as boolean;
|
) as boolean;
|
||||||
|
|
||||||
if (autoDetectResponseFormat) {
|
if (autoDetectResponseFormat) {
|
||||||
const responseContentType = response.headers['content-type'];
|
const responseContentType = response.headers['content-type'] ?? '';
|
||||||
if (responseContentType.includes('application/json')) {
|
if (responseContentType.includes('application/json')) {
|
||||||
responseFormat = 'json';
|
responseFormat = 'json';
|
||||||
response.body = JSON.parse(Buffer.from(response.body).toString());
|
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';
|
responseFormat = 'file';
|
||||||
} else {
|
} else {
|
||||||
responseFormat = 'text';
|
responseFormat = 'text';
|
||||||
|
|
Loading…
Reference in a new issue