basic http auth in zappier code

2019-09-16 14:01发布

问题:

I'm trying to send HTTP request with Zapier Code to hit my API to do some GET and POST requests.

API requires API_KEY in form of authorization header to understand my requests. Here is code I'm running

 var settings = {
      "url": "https://<HOST>/api/v1/siteinfo",
      "method": "GET",
      "headers": {
        "authorization": "Basic <TOKEN>",
        "cache-control": "no-cache"
      }
    }

fetch(settings.url, settings)
.then(function (r) {
  callback({data: r});
}).catch(callback);

But get this error:

What is wrong with my code?

回答1:

It turns out that the first argument of callback function is always error, thus if we have the some result to pass from asynchronous action we should pass null as the first argument to callback, e.g. in my case I should have this:

fetch(settings.url, settings)
.then(function (r) {
  callback(null, {data: r});
}).catch(callback);