mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-13 13:57:29 -08:00
fix(HTTP Request Node): Support form data when using pagination (#8497)
This commit is contained in:
parent
cc41fc7c80
commit
ca75744c7f
|
@ -1208,6 +1208,23 @@ async function prepareBinaryData(
|
||||||
return await setBinaryDataBuffer(returnData, binaryData, workflowId, executionId);
|
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
|
* Makes a request using OAuth data for authentication
|
||||||
*
|
*
|
||||||
|
@ -2806,7 +2823,7 @@ const getRequestHelperFunctions = (
|
||||||
|
|
||||||
let tempResponseData: IN8nHttpFullResponse;
|
let tempResponseData: IN8nHttpFullResponse;
|
||||||
let makeAdditionalRequest: boolean;
|
let makeAdditionalRequest: boolean;
|
||||||
let paginateRequestData: IHttpRequestOptions;
|
let paginateRequestData: PaginationOptions['request'];
|
||||||
|
|
||||||
const runIndex = 0;
|
const runIndex = 0;
|
||||||
|
|
||||||
|
@ -2836,9 +2853,9 @@ const getRequestHelperFunctions = (
|
||||||
executeData,
|
executeData,
|
||||||
additionalKeys,
|
additionalKeys,
|
||||||
false,
|
false,
|
||||||
) as object as IHttpRequestOptions;
|
) as object as PaginationOptions['request'];
|
||||||
|
|
||||||
const tempRequestOptions = merge(requestOptions, paginateRequestData);
|
const tempRequestOptions = applyPaginationRequestData(requestOptions, paginateRequestData);
|
||||||
|
|
||||||
if (credentialsType) {
|
if (credentialsType) {
|
||||||
tempResponseData = await this.helpers.requestWithAuthentication.call(
|
tempResponseData = await this.helpers.requestWithAuthentication.call(
|
||||||
|
|
|
@ -18,6 +18,40 @@ describe('Test HTTP Request Node', () => {
|
||||||
await initBinaryDataService();
|
await initBinaryDataService();
|
||||||
nock.disableNetConnect();
|
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
|
//GET
|
||||||
nock(baseUrl).get('/todos/1').reply(200, {
|
nock(baseUrl).get('/todos/1').reply(200, {
|
||||||
id: 1,
|
id: 1,
|
||||||
|
@ -119,46 +153,36 @@ describe('Test HTTP Request Node', () => {
|
||||||
deletedOn: '2023-02-09T05:37:31.720Z',
|
deletedOn: '2023-02-09T05:37:31.720Z',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Pagination - Data not identical to dummyjson.com
|
// Pagination - GET
|
||||||
nock(baseUrl)
|
nock(baseUrl)
|
||||||
.persist()
|
.persist()
|
||||||
.get('/users')
|
.get('/users')
|
||||||
.query(true)
|
.query(true)
|
||||||
.reply(function (uri) {
|
.reply(function (uri) {
|
||||||
const data = parseUrl(uri, true);
|
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 limit = parseInt((data.query.limit as string) || '10', 10);
|
||||||
const nextUrl = `${baseUrl}/users?skip=${skip + limit}&limit=${limit}`;
|
const skip = parseInt((data.query.skip as string) || '0', 10);
|
||||||
|
return getPaginationReturnData.call(this, limit, skip);
|
||||||
|
});
|
||||||
|
|
||||||
const response = [];
|
// Pagination - POST
|
||||||
for (let i = skip; i < skip + limit; i++) {
|
nock(baseUrl)
|
||||||
if (i > 14) {
|
.persist()
|
||||||
break;
|
.post('/users')
|
||||||
}
|
.reply(function (_uri, body) {
|
||||||
response.push({
|
let skip = 0;
|
||||||
id: i,
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.length) {
|
return getPaginationReturnData.call(this, limit, skip);
|
||||||
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',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue