Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HTTP Request Node): Support form data when using pagination #8497

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,23 @@ async function prepareBinaryData(
return await setBinaryDataBuffer(returnData, binaryData, workflowId, executionId);
}

function applyPaginationRequestData(
requestData: OptionsWithUri,
paginationRequestData: PaginationOptions['request'],
): OptionsWithUri {
const preparedPaginationData: Partial<OptionsWithUri> = { ...paginationRequestData };

if ('formData' in requestData) {
preparedPaginationData.formData = paginationRequestData.body;
delete preparedPaginationData.body;
} else if ('form' in requestData) {
preparedPaginationData.form = paginationRequestData.body;
delete preparedPaginationData.body;
}

return merge({}, requestData, preparedPaginationData);
}

/**
* Makes a request using OAuth data for authentication
*
Expand Down Expand Up @@ -2796,7 +2813,7 @@ const getRequestHelperFunctions = (

let tempResponseData: IN8nHttpFullResponse;
let makeAdditionalRequest: boolean;
let paginateRequestData: IHttpRequestOptions;
let paginateRequestData: PaginationOptions['request'];

const runIndex = 0;

Expand Down Expand Up @@ -2826,9 +2843,9 @@ const getRequestHelperFunctions = (
executeData,
additionalKeys,
false,
) as object as IHttpRequestOptions;
) as object as PaginationOptions['request'];

const tempRequestOptions = merge(requestOptions, paginateRequestData);
const tempRequestOptions = applyPaginationRequestData(requestOptions, paginateRequestData);

if (credentialsType) {
tempResponseData = await this.helpers.requestWithAuthentication.call(
Expand Down
84 changes: 54 additions & 30 deletions packages/nodes-base/nodes/HttpRequest/test/node/HttpRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ describe('Test HTTP Request Node', () => {
await initBinaryDataService();
nock.disableNetConnect();

function getPaginationReturnData(this: nock.ReplyFnContext, limit = 10, skip = 0) {
const nextUrl = `${baseUrl}/users?skip=${skip + limit}&limit=${limit}`;

const response = [];
for (let i = skip; i < skip + limit; i++) {
if (i > 14) {
break;
}
response.push({
id: i,
});
}

if (!response.length) {
return [
404,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
}

return [
200,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
}

//GET
nock(baseUrl).get('/todos/1').reply(200, {
id: 1,
Expand Down Expand Up @@ -119,46 +153,36 @@ describe('Test HTTP Request Node', () => {
deletedOn: '2023-02-09T05:37:31.720Z',
});

// Pagination - Data not identical to dummyjson.com
// Pagination - GET
nock(baseUrl)
.persist()
.get('/users')
.query(true)
.reply(function (uri) {
const data = parseUrl(uri, true);
const skip = parseInt((data.query.skip as string) || '0', 10);
const limit = parseInt((data.query.limit as string) || '10', 10);
const nextUrl = `${baseUrl}/users?skip=${skip + limit}&limit=${limit}`;

const response = [];
for (let i = skip; i < skip + limit; i++) {
if (i > 14) {
break;
}
response.push({
id: i,
});
}
const skip = parseInt((data.query.skip as string) || '0', 10);
return getPaginationReturnData.call(this, limit, skip);
});

if (!response.length) {
return [
404,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
// Pagination - POST
nock(baseUrl)
.persist()
.post('/users')
.reply(function (_uri, body) {
let skip = 0;
let limit = 10;

if (typeof body === 'string') {
// Form data
skip = parseInt(body.split('name="skip"')[1].split('---')[0] ?? '0', 10);
limit = parseInt(body.split('name="limit"')[1].split('---')[0] ?? '0', 10);
} else {
skip = parseInt(body.skip ?? '0', 10);
limit = parseInt(body.limit ?? '10', 10);
}

return [
200,
response,
{
'next-url': nextUrl,
'content-type': this.req.headers['content-type'] || 'application/json',
},
];
return getPaginationReturnData.call(this, limit, skip);
});
});

Expand Down
Loading
Loading