Mar 28, 2023 07:41 AM
So, with my limited scripting and Java skills I've been trying to set up a script that will call a webhook so I can later trigger something there. I managed to set up the basic ping but I came across the ideal of sending some record ID's I need which can save me a step later on.
My error is surrounding inputConfig and how to parse it into the JSON body (I guess?)
The error most likely lies in line 4 and 5, hopefully it's understandable what I'm trying to get done. For line 4 and 5 I also tried "" but that simply sends over the whole thing and not the input I setup in the script (picture attached below)
let inputConfig = input.config();
let body = {
"ping": "Survey Ping",
"company_id":${inputConfig.contact_id},
"contact_id":${inputConfig.company_id}
}
let response = await fetch('https://n8n.joinowners.co/webhook-test/tally-onboardinglaunch-survey', {
method: 'POST',
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},
});
console.log(await response.json());
Solved! Go to Solution.
Mar 28, 2023 08:43 AM
As some additional insight into why your original script wasn't working, you were trying to insert the values of inputConfig.contact_id and inputConfig.company_id using template literals, but you forgot to properly enclose the values inside of backticks. So you'd switch it from this:
let body = {
"ping": "Survey Ping",
"company_id": ${inputConfig.contact_id},
"contact_id": ${inputConfig.company_id}
}
To this:
let body = {
"ping": "Survey Ping",
"company_id": `${inputConfig.contact_id}`,
"contact_id": `${inputConfig.company_id}`
}
You actually don't have to do this at all. You can actually just simply invoke the properties without enclosing them inside of the template literals like this:
let body = {
"ping": "Survey Ping",
"company_id": inputConfig.contact_id,
"contact_id": inputConfig.company_id
}
Mar 28, 2023 08:36 AM - edited Mar 28, 2023 08:36 AM
Hey @Manuel-Tanco!
Give this a shot:
const config = input.config();
const { company_id, contact_id } = config;
const body = {
ping: "Survey Ping",
company_id: company_id,
contact_id: contact_id
};
const url = "https://n8n.joinowners.co/webhook-test/tally-onboardinglaunch-survey";
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(body)
};
let data = await fetch(url, options)
.then(response => {
console.log(response);
return response.json();
});
console.log(data)
Mar 28, 2023 08:43 AM
As some additional insight into why your original script wasn't working, you were trying to insert the values of inputConfig.contact_id and inputConfig.company_id using template literals, but you forgot to properly enclose the values inside of backticks. So you'd switch it from this:
let body = {
"ping": "Survey Ping",
"company_id": ${inputConfig.contact_id},
"contact_id": ${inputConfig.company_id}
}
To this:
let body = {
"ping": "Survey Ping",
"company_id": `${inputConfig.contact_id}`,
"contact_id": `${inputConfig.company_id}`
}
You actually don't have to do this at all. You can actually just simply invoke the properties without enclosing them inside of the template literals like this:
let body = {
"ping": "Survey Ping",
"company_id": inputConfig.contact_id,
"contact_id": inputConfig.company_id
}
Mar 28, 2023 10:01 AM
Okay understood, I was having a hard time trying to get synced with the correct way of writing it down. Thank you @Ben_Young1 !