Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can "fetch" in code by Zapier perform a GET & POST?

Tags:

fetch

zapier

In Zapier, I've set up the zap "Schedule by Zapier" as trigger and "Code by Zapier" as action. Inside the action, "Code by Zapier" I'd like to perform the following: GET an URL & POST to another URL. However, when I use "fetch" in Zapier from this documentation (https://github.com/bitinn/node-fetch/tree/32b60634434a63865ea3f79edb33d17e40876c9f#usage) the first request (GET) already took 900ms and to do the second request means it would take more than 1sec to do the action. Zapier doesn't like this. Can anyone help? Thanks, Elco

like image 948
ElcoIan Avatar asked Oct 29 '25 05:10

ElcoIan


1 Answers

I was able to do this by chaining together two Code zaps. The first zap does the get (grab a random number from random.org):

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

This call will return a variable called "rawHTML" that you can use in the next part of the chain.


"Run 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);
    });

It is much much easier to debug this stuff if you use http://requestb.in before you target your real end point.

like image 181
Matt Dreyer Avatar answered Oct 31 '25 12:10

Matt Dreyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!