Fixed GET method 400 response

This commit is contained in:
Rupenieks 2020-05-02 13:59:17 +02:00
parent 9ac9ff3557
commit aca8441164
3 changed files with 12 additions and 9 deletions

View file

@ -76,7 +76,7 @@ export class AgileCrm implements INodeType {
const contactId = this.getNodeParameter('contactId', i) as string; const contactId = this.getNodeParameter('contactId', i) as string;
const endpoint = `api/contacts/${contactId}`; const endpoint = `api/contacts/${contactId}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} }
@ -85,11 +85,11 @@ export class AgileCrm implements INodeType {
if (returnAll) { if (returnAll) {
const endpoint = `api/contacts`; const endpoint = `api/contacts`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} else { } else {
const limit = this.getNodeParameter('limit', i) as number; const limit = this.getNodeParameter('limit', i) as number;
const endpoint = `api/contacts?page_size=${limit}`; const endpoint = `api/contacts?page_size=${limit}`;
responseData = await agileCrmApiRequest.call(this, 'GET', endpoint); responseData = await agileCrmApiRequest.call(this, 'GET', endpoint, {});
} }
} }

View file

@ -14,17 +14,14 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}, uri?: string): Promise<any> { export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query: IDataObject = {}, uri?: string): Promise<any> {
const node = this.getNodeParameter('credentials', 1);
const credentials = this.getCredentials('agileCrmApi'); const credentials = this.getCredentials('agileCrmApi');
const options: OptionsWithUri = { const options: OptionsWithUri = {
method, method,
headers: { headers: {
'Accept': 'application/json', 'Accept': 'application/json',
}, },
body: body! || {},
auth: { auth: {
username: credentials!.email as string, username: credentials!.email as string,
password: credentials!.apiKey as string password: credentials!.apiKey as string
@ -32,8 +29,14 @@ export async function agileCrmApiRequest(this: IHookFunctions | IExecuteFunction
uri: uri || `https://n8nio.agilecrm.com/dev/${endpoint}`, uri: uri || `https://n8nio.agilecrm.com/dev/${endpoint}`,
json: true json: true
}; };
// Only add Body property if method not GET to avoid 400 response
if(method !== "GET"){
options.body = body;
}
console.log(options);
try { try {
return await this.helpers.request!(options); return await this.helpers.request!(options);
} catch (error) { } catch (error) {

View file

@ -27,7 +27,7 @@
"n8n": { "n8n": {
"credentials": [ "credentials": [
"dist/credentials/ActiveCampaignApi.credentials.js", "dist/credentials/ActiveCampaignApi.credentials.js",
"dist/credentials/AgileCrm.credentials.js", "dist/credentials/AgileCrmApi.credentials.js",
"dist/credentials/AcuitySchedulingApi.credentials.js", "dist/credentials/AcuitySchedulingApi.credentials.js",
"dist/credentials/AirtableApi.credentials.js", "dist/credentials/AirtableApi.credentials.js",
"dist/credentials/Amqp.credentials.js", "dist/credentials/Amqp.credentials.js",