Zappier代码中的基本http auth[英] basic http auth in zappier code

本文是小编为大家收集整理的关于Zappier代码中的基本http auth的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试发送带有Zapier代码的HTTP请求以达到我的API来执行一些获取和发布请求.

在此处输入图像说明

api需要 api_key 以授权的形式标题以了解我的请求.这是我正在运行的代码

 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);

但是获取此错误:

在此处输入图像说明

我的代码有什么问题?

推荐答案

事实证明,回调函数的第一个参数始终是错误的,因此,如果我们有一些结果可以从异步操作传递,我们应该将null作为第一个参数传递给callback,例如就我而言,我应该有:

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

本文地址:https://www.itbaoku.cn/post/1937782.html

问题描述

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

enter image description here

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:

enter image description here

What is wrong with my code?

推荐答案

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);