Skip to content

Commit

Permalink
feat(HTTP Request Node): add option for raw json header & query (n8n-…
Browse files Browse the repository at this point in the history
…io#4408)

* http node headers and query JSON parameter options added

* Query and Header added to option processing

* remove old set headers.

* ⚡ Small improvements

Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
  • Loading branch information
bramkn and RicardoE105 authored Oct 21, 2022
1 parent 46a43e4 commit e69c333
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 36 deletions.
62 changes: 31 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

122 changes: 117 additions & 5 deletions packages/nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,35 @@ export class HttpRequestV3 implements INodeType {
noDataExpression: true,
description: 'Whether the request has query params or not',
},
{
displayName: 'Specify Query Parameters',
name: 'specifyQuery',
type: 'options',
displayOptions: {
show: {
sendQuery: [true],
},
},
options: [
{
name: 'Using Fields Below',
value: 'keypair',
},
{
name: 'Using JSON',
value: 'json',
},
],
default: 'keypair',
},
{
displayName: 'Query Parameters',
name: 'queryParameters',
type: 'fixedCollection',
displayOptions: {
show: {
sendQuery: [true],
specifyQuery: ['keypair'],
},
},
typeOptions: {
Expand Down Expand Up @@ -186,6 +208,18 @@ export class HttpRequestV3 implements INodeType {
},
],
},
{
displayName: 'JSON',
name: 'jsonQuery',
type: 'json',
displayOptions: {
show: {
sendQuery: [true],
specifyQuery: ['json'],
},
},
default: '',
},
{
displayName: 'Send Headers',
name: 'sendHeaders',
Expand All @@ -194,13 +228,35 @@ export class HttpRequestV3 implements INodeType {
noDataExpression: true,
description: 'Whether the request has headers or not',
},
{
displayName: 'Specify Headers',
name: 'specifyHeaders',
type: 'options',
displayOptions: {
show: {
sendHeaders: [true],
},
},
options: [
{
name: 'Using Fields Below',
value: 'keypair',
},
{
name: 'Using JSON',
value: 'json',
},
],
default: 'keypair',
},
{
displayName: 'Header Parameters',
name: 'headerParameters',
type: 'fixedCollection',
displayOptions: {
show: {
sendHeaders: [true],
specifyHeaders: ['keypair'],
},
},
typeOptions: {
Expand Down Expand Up @@ -236,6 +292,18 @@ export class HttpRequestV3 implements INodeType {
},
],
},
{
displayName: 'JSON',
name: 'jsonHeaders',
type: 'json',
displayOptions: {
show: {
sendHeaders: [true],
specifyHeaders: ['json'],
},
},
default: '',
},
{
displayName: 'Send Body',
name: 'sendBody',
Expand Down Expand Up @@ -865,6 +933,9 @@ export class HttpRequestV3 implements INodeType {
itemIndex,
[],
) as [{ name: string; value: string }];
const specifyQuery = this.getNodeParameter('specifyQuery', itemIndex, 'keypair') as string;
const jsonQueryParameter = this.getNodeParameter('jsonQuery', itemIndex, '') as string;

const sendBody = this.getNodeParameter('sendBody', itemIndex, false) as boolean;
const bodyContentType = this.getNodeParameter('contentType', itemIndex, '') as string;
const specifyBody = this.getNodeParameter('specifyBody', itemIndex, '') as string;
Expand All @@ -880,6 +951,8 @@ export class HttpRequestV3 implements INodeType {
itemIndex,
[],
) as [{ name: string; value: string }];
const specifyHeaders = this.getNodeParameter('specifyHeaders', itemIndex, 'keypair') as string;
const jsonHeadersParameter = this.getNodeParameter('jsonHeaders', itemIndex, '') as string;

const {
redirect,
Expand Down Expand Up @@ -1053,15 +1126,54 @@ export class HttpRequestV3 implements INodeType {
}
}

// Get parameters defined in the UI
if (sendQuery && queryParameters) {
requestOptions.qs = await queryParameters.reduce(parmetersToKeyValue, Promise.resolve({}));
if (specifyQuery === 'keypair') {
requestOptions.qs = await queryParameters.reduce(
parmetersToKeyValue,
Promise.resolve({}),
);
} else if (specifyQuery === 'json') {
// query is specified using JSON
try {
JSON.parse(jsonQueryParameter);
} catch (_) {
throw new NodeOperationError(
this.getNode(),
`JSON parameter need to be an valid JSON`,
{
runIndex: itemIndex,
},
);
}

requestOptions.qs = JSON.parse(jsonQueryParameter);
}
}

// Get parameters defined in the UI
if (sendHeaders && headerParameters) {
requestOptions.headers = await headerParameters.reduce(
parmetersToKeyValue,
Promise.resolve({}),
);
if (specifyHeaders === 'keypair') {
requestOptions.headers = await headerParameters.reduce(
parmetersToKeyValue,
Promise.resolve({}),
);
} else if (specifyHeaders === 'json') {
// body is specified using JSON
try {
JSON.parse(jsonHeadersParameter);
} catch (_) {
throw new NodeOperationError(
this.getNode(),
`JSON parameter need to be an valid JSON`,
{
runIndex: itemIndex,
},
);
}

requestOptions.headers = JSON.parse(jsonHeadersParameter);
}
}

if (autoDetectResponseFormat || responseFormat === 'file') {
Expand Down

0 comments on commit e69c333

Please sign in to comment.