Small improvement to HTTP Request-Node

This commit is contained in:
Jan Oberhauser 2020-04-10 00:44:09 +02:00
parent d32b4bbaa8
commit 4374b3d914

View file

@ -168,20 +168,6 @@ export class HttpRequest implements INodeType {
default: 'json', default: 'json',
description: 'The format in which the data gets returned from the URL.', description: 'The format in which the data gets returned from the URL.',
}, },
{
displayName: 'Parse to JSON',
name: 'parseToJson',
type: 'boolean',
default: false,
displayOptions: {
show: {
responseFormat: [
'string',
],
},
},
description: 'Parse the response to JSON',
},
{ {
displayName: 'Property Name', displayName: 'Property Name',
name: 'dataPropertyName', name: 'dataPropertyName',
@ -193,9 +179,6 @@ export class HttpRequest implements INodeType {
responseFormat: [ responseFormat: [
'string', 'string',
], ],
parseToJson: [
false,
],
}, },
}, },
description: 'Name of the property to which to write the response data.', description: 'Name of the property to which to write the response data.',
@ -715,14 +698,12 @@ export class HttpRequest implements INodeType {
} }
try { try {
JSON.parse(tempValue as string); // @ts-ignore
requestOptions[optionData.name] = JSON.parse(tempValue as string);
} catch (error) { } catch (error) {
throw new Error(`${optionData.name} must be a valid JSON`); throw new Error(`${optionData.name} must be a valid JSON`);
} }
// @ts-ignore
requestOptions[optionData.name] = JSON.parse(tempValue);
// @ts-ignore // @ts-ignore
if (typeof requestOptions[optionData.name] !== 'object' && options.bodyContentType !== 'raw') { if (typeof requestOptions[optionData.name] !== 'object' && options.bodyContentType !== 'raw') {
// If it is not an object && bodyContentType is not 'raw' it must be JSON so parse it // If it is not an object && bodyContentType is not 'raw' it must be JSON so parse it
@ -855,22 +836,13 @@ export class HttpRequest implements INodeType {
items[itemIndex] = newItem; items[itemIndex] = newItem;
} else if (responseFormat === 'string') { } else if (responseFormat === 'string') {
const parseToJson = this.getNodeParameter('parseToJson', 0) as string; const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
let dataPropertyName = '';
if (!parseToJson) {
dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
}
if (fullResponse === true) { if (fullResponse === true) {
let returnItem: IDataObject = {}; const returnItem: IDataObject = {};
for (const property of fullReponseProperties) { for (const property of fullReponseProperties) {
if (property === 'body') { if (property === 'body') {
if (!parseToJson) { returnItem[dataPropertyName] = response[property];
returnItem[dataPropertyName] = response[property];
} else {
returnItem = JSON.parse(response.property);
}
continue; continue;
} }
@ -878,14 +850,10 @@ export class HttpRequest implements INodeType {
} }
returnItems.push({ json: returnItem }); returnItems.push({ json: returnItem });
} else { } else {
let output: IDataObject = {};
if (!parseToJson) {
output = { [dataPropertyName]: response };
} else {
output = JSON.parse(response);
}
returnItems.push({ returnItems.push({
json: output, json: {
[dataPropertyName]: response,
},
}); });
} }
} else { } else {
@ -896,14 +864,22 @@ export class HttpRequest implements INodeType {
returnItem[property] = response[property]; returnItem[property] = response[property];
} }
if (typeof returnItem.body === 'string') { if (responseFormat === 'json' && typeof returnItem.body === 'string') {
throw new Error('Response body is not valid JSON. Change "Response Format" to "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 }); returnItems.push({ json: returnItem });
} else { } else {
if (typeof response === 'string') { if (responseFormat === 'json' && typeof response === 'string') {
throw new Error('Response body is not valid JSON. Change "Response Format" to "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 }); returnItems.push({ json: response });