Jeff Hall
Jeff Hall

Reputation: 1

Posting to API via zapier code and getting this error

I am trying to run a JSON post to an API I am working with Via Zapier Code.

I have defined my inputs and this is my code below. It seems to get through most of it without any syntax errors. But Zapier is now giving me this error response and I cannot figure out why?

"We had trouble sending your test through. Please try again. Error: You must return a single object or array of objects."

Does anyone have any input on this?

const API_URL = "https://myapi.com";

const DATA = {
  siteId: "xxxx",
  id: inputData.orderId,
  totalATI: 39.99,
  totalET: 39.99,
  currency: "USD",
  accountId: inputData.accountId,
  ip: inputData.userIP,
  recoverUrl: "",
  civility: "",
  lastname: inputData.lastname,
  firstname: inputData.firstname,
  email: inputData.accountId,
  homePhoneNumber: "",
  mobilePhoneNumber: "",
  phoneNumber: inputData.userPhone,
  countryCode: "01",
  custom:{},
  
  items: [{
  id: "88",
  label: "CR",
  quantity: 1,
  totalATI: 39.99,
  totalET: 39.99,
  url: "https://myurl.com",
  imageUrl: "https://myimage.com",
  universe: "",
  category:  ""
  }]
};

fetch(API_URL, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'X-API-Key': 'xxx'
  },
  body: JSON.stringify(DATA)
}).then(function(res){
  return res.json();
}).then(function(response){
  // API response
  if(response.success){
    // Tell zapier success
    callback(null, {
      result: "success",
      message: "Request successful!",
      data: response
    });
  }
  // Some error happened.
 else {callback(response)};

}).catch(callback);

Upvotes: 0

Views: 1486

Answers (1)

KayCee
KayCee

Reputation: 174

I believe what is happening here is that the response from the API is not a proper JSON which is why the call is being made to the API but the fetch library is unable to parse the response.

For example, this piece of code is to parse a JSON response.

fetch(API_URL, {
    method: 'POST',
    body: JSON.stringify(DATA),
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-API-Key': 'xxx'
    }
})
    .then(function(res) {
        return res.json();
    })
    .then(function(body) {
        console.log(body);
        callback(null, body.json);
    }).catch(callback);

This code will error out when the response is NOT a proper JSON.

You could try changing your code to the following where you are considering the response to be in text.

fetch(API_URL, {
    method: 'POST',
    body: JSON.stringify(DATA),
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-API-Key': 'xxx'
    }
})
    .then(function(res) {
        return res.text();
    })
    .then(function(body) {
        console.log(body);
        var output = {rawHTML: body};
        callback(null, output);
    })
    .catch(callback);

I've posted a similar answer here.

Upvotes: 1

Related Questions