how to trigger webhook from zapier code

2019-08-16 09:46发布

问题:

I have 2 zaps. First finishes with Code by Zapier block, where I parse input information from previous steps getting array with data e.g.:

var elements = [{id: 12, calculatedValue: 13},{id: 13, calculatedValue: 'red'}]

then in a loop I traverse it, create requests bodies

var options = {
      "url": "https://hooks.zapier.com/hooks/catch/xxxxxx/xxxxxx/",
      "method": "POST"
    },
    requests = elements.map(mapDataToSettings);

function mapDataToSettings(elem) {
  var settings = Object.assign({}, options);
  settings.data = JSON.stringify(elem);
  return settings;
};

Then I'm doing HTTP calls with Fetch API for all those requests:

Promise.all(requests.map(grabContent))
.then(function(data){ callback(null, {requestsMade: data});});

function grabContent(options) {
  return fetch(options.url, options)
     .then(function(res) {return res.json();});
};

N.B. callback is function of Zapier to handle async results.

This code successfully runs and I can see results:

But those requests are not registered in webhook (address is correct. double checked.)

What may be the reason for this? How to fix my code to make requests activate webhook?

回答1:

It might be that you don't have a body item in your options that get sent with the fetch method. The documentation shows this as a POST example: { method: 'POST', body: 'a=1' }, so maybe try to make it exactly like that.