Best answer

Calling an API method that returns an empty OK (200) or Bad Request (400)

  • 4 November 2020
  • 3 replies
  • 414 views

Userlevel 1

Hi,

 

Zapier allows Create and Search actions. But what about Do type actions? I have an API method that will be used at the end of a Zap (so does not need to pass anything to another action step).My API currently either returns an empty OK (200) or a BadRequest (400) with the error as a string.

I currently get this:

“Invalid API Response: - Got a non-object result, expected an object from create”

How do I go about creating the action in the Zapier developer console re: dealing with the return result? I guess I have to switch to code mode but what would my code look like to deal with errors and OKs?

My code currently looks like this:

...

return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = response.json;

    // You can do any parsing you need for results here before returning them

    return results;
  });

 

Thank you,

Steve

icon

Best answer by ikbelkirasan 4 November 2020, 17:46

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Userlevel 7
Badge +10

Paging @ikbelkirasan for this.

Userlevel 7
Badge +11

Hi @borgs_uk - How about this, does it solve the problem?

return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
if (!results) {
return {
response: "OK",
};
}
return results;
});

 

Userlevel 1

Hi @borgs_uk - How about this, does it solve the problem?

return z.request(options).then((response) => {
response.throwForStatus();
const results = response.json;
if (!results) {
return {
response: "OK",
};
}
return results;
});

 

Many thanks to you both. That is doing the trick nicely. And, if a 400 error is returned by my API then the throwForStatus is being triggered and leading to me to getting a Zapier alert for the failed Zap - which is good.

Thank you.