Hey All,
I’m attempting to use a code step to make a GET request from a REST API, but I’m running into a 401 authorization error in the response section of the try/catch piece.
However, the same code/credentials work when I test via Postman and via the testing page of the API documentation itself, so I know the credentials themselves are correct and are unchanged between Zapier/Postman. I really don’t know what other options I have to try at this point and am really at a loss as to where this piece is failing since it works fine elsewhere.
It seems like maybe the requestOptions piece isn’t actually getting properly added/pushed through with the Auth header, but I don’t know why that would be happening either and am just kind of at a loss. Does anyone have experience with the particulars of Zapier JS code steps that knows why this request could be failing to Authorize only within Zapier?
(I know the code after that needs work to properly parse the data, I’m not really worried about that and I know I can work out that piece once I have the actual GET request data to come through with a little effort and googling.)
Code Step
const ln = inputData.location_name;
const cf = inputData.customer_filter;
const myHeaders = new Headers();
myHeaders.append("Authorization", "Basic MYAPICREDENTIALS");
const requestOptions = {
method: "GET",
headers: myHeaders,
redirect: "follow"
};
try {
const response = await fetch("https://cloud.servicebridge.com/api/v4.5/Locations?includeContacts=True&locationNameFilter=" + ln + "&customerFilter=" + cf, requestOptions);
console.log(response);
const result = await JSON.parse(response);
console.log(result);
const contact = result.data.contacts;
return { contact_role: contact };
} catch (error) {
console.error(error);
return { output: { error: error.message } }
}
Postman
