Help

SyntaxError in fetch external API

Solved
Jump to Solution
2131 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Richard_Quintan
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello there,

I'm trying to fetch data from a product in one base, then send that information to an external API and then update that new info in other base. The code goes like this:

 

//input of modified product

let inputConfig = input.config();

//JSON creation
let body = {
        "costo_ult": inputConfig.costo_ult[0],
        "disponible": inputConfig.disp[0],
        "dias_obs": inputConfig.dias_obs[0] 
}

console.log(body);
console.log(JSON.stringify(body));

//Send JSON to API
try {
  let response = await fetch(`http://167..../${inputConfig.id[0]}`, {
    method: 'PATCH',
    body: JSON.stringify(body),
    headers: {
      'Origin': '',
      'Content-Type': 'application/json ; charset=utf-8',
      'Authorization': 'eyJhb...',
    },
});
  console.log(await response.json());
    }
catch (error) { 
  console.log(error);
     } 

 

The fields I want to update are related to costs, quantities and rotation days, and the response from the script is

  1. Error {name: "SyntaxError"}
    1.  name: "SyntaxError"

 

So, can anyone explain me why this is happening and how to solve it? Thanks!

1 Solution

Accepted Solutions
marks
Airtable Employee
Airtable Employee

Hi Richard - Have you tried hard-coding values (instead of referencing `inputConfig`) to see if that helps troubleshoot?


When I did that, and tested with an endpoint from https://webhook.site/ that returned proper JSON, I did not get an error. You can also try changing your `await response.json()` to `await response.text()` to see if perhaps the issue is coming up since your endpoint is not returning JSON as you expected.

See Solution in Thread

2 Replies 2
marks
Airtable Employee
Airtable Employee

Hi Richard - Have you tried hard-coding values (instead of referencing `inputConfig`) to see if that helps troubleshoot?


When I did that, and tested with an endpoint from https://webhook.site/ that returned proper JSON, I did not get an error. You can also try changing your `await response.json()` to `await response.text()` to see if perhaps the issue is coming up since your endpoint is not returning JSON as you expected.

Richard_Quintan
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello there @marks and thanks for the reply!

As you suggested, I tried hard-coding values and I discovered that the resource was missing while fetching the API's link. Everything's working fine now!