mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -08:00
⚡ HTTP node improvements
This commit is contained in:
parent
89a73d4653
commit
cbf4a921e8
|
@ -168,7 +168,20 @@ 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',
|
||||||
|
@ -180,6 +193,9 @@ 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.',
|
||||||
|
@ -634,9 +650,9 @@ export class HttpRequest implements INodeType {
|
||||||
// Paramter is empty so skip it
|
// Paramter is empty so skip it
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
const sendBinaryData = this.getNodeParameter('sendBinaryData', itemIndex, false) as boolean;
|
||||||
|
|
||||||
if (optionData.name === 'body' && parametersAreJson === true) {
|
if (optionData.name === 'body' && parametersAreJson === true) {
|
||||||
const sendBinaryData = this.getNodeParameter('sendBinaryData', itemIndex, false) as boolean;
|
|
||||||
if (sendBinaryData === true) {
|
if (sendBinaryData === true) {
|
||||||
|
|
||||||
const contentTypesAllowed = [
|
const contentTypesAllowed = [
|
||||||
|
@ -698,8 +714,14 @@ export class HttpRequest implements INodeType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
JSON.parse(tempValue as string);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`${optionData.name} must be a valid JSON`);
|
||||||
|
}
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
requestOptions[optionData.name] = tempValue;
|
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') {
|
||||||
|
@ -767,6 +789,7 @@ export class HttpRequest implements INodeType {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseFormat === 'json') {
|
if (responseFormat === 'json') {
|
||||||
|
|
||||||
requestOptions.headers!['accept'] = 'application/json,text/*;q=0.99';
|
requestOptions.headers!['accept'] = 'application/json,text/*;q=0.99';
|
||||||
} else if (responseFormat === 'string') {
|
} else if (responseFormat === 'string') {
|
||||||
requestOptions.headers!['accept'] = 'application/json,text/html,application/xhtml+xml,application/xml,text/*;q=0.9, */*;q=0.1';
|
requestOptions.headers!['accept'] = 'application/json,text/html,application/xhtml+xml,application/xml,text/*;q=0.9, */*;q=0.1';
|
||||||
|
@ -781,7 +804,6 @@ export class HttpRequest implements INodeType {
|
||||||
} else {
|
} else {
|
||||||
requestOptions.json = true;
|
requestOptions.json = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Now that the options are all set make the actual http request
|
// Now that the options are all set make the actual http request
|
||||||
response = await this.helpers.request(requestOptions);
|
response = await this.helpers.request(requestOptions);
|
||||||
|
@ -833,13 +855,22 @@ export class HttpRequest implements INodeType {
|
||||||
|
|
||||||
items[itemIndex] = newItem;
|
items[itemIndex] = newItem;
|
||||||
} else if (responseFormat === 'string') {
|
} else if (responseFormat === 'string') {
|
||||||
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
|
const parseToJson = this.getNodeParameter('parseToJson', 0) as string;
|
||||||
|
|
||||||
|
let dataPropertyName = '';
|
||||||
|
if (!parseToJson) {
|
||||||
|
dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
|
||||||
|
}
|
||||||
|
|
||||||
if (fullResponse === true) {
|
if (fullResponse === true) {
|
||||||
const returnItem: IDataObject = {};
|
let returnItem: IDataObject = {};
|
||||||
for (const property of fullReponseProperties) {
|
for (const property of fullReponseProperties) {
|
||||||
if (property === 'body') {
|
if (property === 'body') {
|
||||||
returnItem[dataPropertyName] = response[property];
|
if (!parseToJson) {
|
||||||
|
returnItem[dataPropertyName] = response[property];
|
||||||
|
} else {
|
||||||
|
returnItem = JSON.parse(response.property);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -847,10 +878,14 @@ 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: {
|
json: output,
|
||||||
[dataPropertyName]: response,
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue