可以"获取"在Zapier的代码中执行GET& POST?

时间:2015-12-18 13:14:16

标签: fetch zapier

在Zapier,我已经设置了Zapier" Schedule by Zapier"作为触发器和"代码由Zapier"作为行动。在行动中,"代码由Zapier"我想执行以下操作:获取网址& POST到另一个URL。但是,当我使用" fetch"在Zapier中,从这个文档(https://github.com/bitinn/node-fetch/tree/32b60634434a63865ea3f79edb33d17e40876c9f#usage)第一个请求(GET)已经花了900毫秒而第二个请求意味着需要超过1秒 做这个动作。扎皮尔并不喜欢这样。有人可以帮忙吗?谢谢,埃尔科

1 个答案:

答案 0 :(得分:1)

我能够通过将两个Code zaps链接在一起来实现这一点。第一个zap执行get(从random.org获取一个随机数):

"运行Javascript#1"



fetch('https://www.random.org/passwords/?num=1&len=24&format=plain&rnd=new')
    .then(function(res) {
    return res.text();
    })
    .then(function(body) {
        var output = {id: 1234, rawHTML: body};
        callback(null, output);
    })
    .catch(callback);




此调用将返回一个名为" rawHTML"的变量。您可以在链的下一部分使用。

"运行Javascript 2" Screenshot of inputData variables



//random.org includes an extra \n in the password, need to clean that up
var cleanpassword = inputData.strPassword.replace(/\n/g, '');

var payload = {firstName: inputData.strFirstName, lastName: inputData.strLastName, username: inputData.strUserName, password: cleanpassword};

var testendpoint = 'http://requestb.in/s523eys5';
//var test = JSON.stringify(payload);
//console.log(test);
     
fetch(testendpoint, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(payload)
    }).then(function(response) {
        return response.text();
    }).then(function(responsebody) {
        var output = {response: responsebody};
        callback(null, output);
    }).catch(function(error) {
        callback(error);
    });




如果在定位真实终点之前使用http://requestb.in,则调试此内容要容易得多。

相关问题