LoginSignup
0
0

More than 3 years have passed since last update.

Node.js(12.x)でfetchを使ってIFTTTのWebhooksへPOST【備忘録】

Posted at

Node.jsでfetchを使ってIFTTTのWebhooksへPOSTするには?

その通りの事をしたかったのだが、記事があまり見当たらなかった(検索が下手なのかも)ので備忘録として認めておく。
前提として、IFTTTのWebhooksに登録し、キーとイベント名を持っている状態とする。

const fetch = require('node-fetch');

const send2IFTTTWebHooks = async (j) => {
    const key = "XXXXXXXX";
    const event = "XXXXXXXX";
    const options =
    {
        "method":"POST",
        "headers":
        {
            "Content-Type":"application/json"
        },
        "body": JSON.stringify(j)
    };
    console.log("send ===> " + JSON.stringify(j));
    // "send ===> { "value1" : "123", "value2" : "456", "value3" : "789" }"
    const ret = await fetch("https://maker.ifttt.com/trigger/" + event + "/with/key/" + key, options);
    console.log("ret ===> " + JSON.stringify(ret.ok));
   // "ret ===> true"(成功の場合)
};

const main = async () => {
    const json = { "value1" : "123", "value2" : "456", "value3" : "789" };
    await send2IFTTTWebHooks(json);
};

main();

結局、fetchの使い方なのだが、意外と忘れてしまう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0