diff --git a/packages/nodes-base/nodes/HttpRequest.node.ts b/packages/nodes-base/nodes/HttpRequest.node.ts index 5afc2bb74..2186a2f22 100644 --- a/packages/nodes-base/nodes/HttpRequest.node.ts +++ b/packages/nodes-base/nodes/HttpRequest.node.ts @@ -168,7 +168,6 @@ export class HttpRequest implements INodeType { default: 'json', description: 'The format in which the data gets returned from the URL.', }, - { displayName: 'Property Name', name: 'dataPropertyName', @@ -634,9 +633,9 @@ export class HttpRequest implements INodeType { // Paramter is empty so skip it continue; } + const sendBinaryData = this.getNodeParameter('sendBinaryData', itemIndex, false) as boolean; if (optionData.name === 'body' && parametersAreJson === true) { - const sendBinaryData = this.getNodeParameter('sendBinaryData', itemIndex, false) as boolean; if (sendBinaryData === true) { const contentTypesAllowed = [ @@ -698,8 +697,12 @@ export class HttpRequest implements INodeType { } } - // @ts-ignore - requestOptions[optionData.name] = tempValue; + try { + // @ts-ignore + requestOptions[optionData.name] = JSON.parse(tempValue as string); + } catch (error) { + throw new Error(`${optionData.name} must be a valid JSON`); + } // @ts-ignore if (typeof requestOptions[optionData.name] !== 'object' && options.bodyContentType !== 'raw') { @@ -767,6 +770,7 @@ export class HttpRequest implements INodeType { } if (responseFormat === 'json') { + requestOptions.headers!['accept'] = 'application/json,text/*;q=0.99'; } else if (responseFormat === 'string') { requestOptions.headers!['accept'] = 'application/json,text/html,application/xhtml+xml,application/xml,text/*;q=0.9, */*;q=0.1'; @@ -781,7 +785,6 @@ export class HttpRequest implements INodeType { } else { requestOptions.json = true; } - try { // Now that the options are all set make the actual http request response = await this.helpers.request(requestOptions); @@ -850,7 +853,7 @@ export class HttpRequest implements INodeType { returnItems.push({ json: { [dataPropertyName]: response, - } + }, }); } } else { @@ -861,14 +864,22 @@ export class HttpRequest implements INodeType { returnItem[property] = response[property]; } - if (typeof returnItem.body === 'string') { - throw new Error('Response body is not valid JSON. Change "Response Format" to "String"'); + if (responseFormat === 'json' && typeof returnItem.body === 'string') { + try { + returnItem.body = JSON.parse(returnItem.body); + } catch (e) { + throw new Error('Response body is not valid JSON. Change "Response Format" to "String"'); + } } returnItems.push({ json: returnItem }); } else { - if (typeof response === 'string') { - throw new Error('Response body is not valid JSON. Change "Response Format" to "String"'); + if (responseFormat === 'json' && typeof response === 'string') { + try { + response = JSON.parse(response); + } catch (e) { + throw new Error('Response body is not valid JSON. Change "Response Format" to "String"'); + } } returnItems.push({ json: response });