Skip to content

Instantly share code, notes, and snippets.

@navdeepsingh
Last active February 21, 2019 06:07
Show Gist options
  • Save navdeepsingh/07c77d89165151a1ee9ca8559ac51d16 to your computer and use it in GitHub Desktop.
Save navdeepsingh/07c77d89165151a1ee9ca8559ac51d16 to your computer and use it in GitHub Desktop.

Revisions

  1. navdeepsingh revised this gist Feb 21, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions postToOmnisend.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    /*****************/
    //@TODO Update Omnisend separately with list id
    //@TODO Update Omnisend separately with list id
    /*****************/

    An object of options to indicate where to post to
    // An object of options to indicate where to post to
    var options = {
    "method": "POST",
    "protocol": 'https:',
  2. navdeepsingh created this gist Feb 21, 2019.
    47 changes: 47 additions & 0 deletions postToOmnisend.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    /*****************/
    //@TODO Update Omnisend separately with list id
    /*****************/

    An object of options to indicate where to post to
    var options = {
    "method": "POST",
    "protocol": 'https:',
    "hostname": 'api.omnisend.com',
    "path": '/v3/contacts',
    "headers": {
    "Content-Type": "application/json",
    "X-API-KEY": process.env.OMNISEND_API_KEY
    }
    };

    let postData = {
    email: email,
    lists: [{ listID: process.env.OMNISEND_LIST_ID }],
    status: 'subscribed',
    statusDate: new Date().toISOString()
    }

    let stringPostData = JSON.stringify(postData);
    console.log(stringPostData);

    //Send post request to omnisend
    var req = http.request(options, function (res) {
    var chunks = [];

    res.on("data", function (chunk) {
    chunks.push(chunk);
    });

    res.on("end", function () {
    var body = Buffer.concat(chunks);
    console.log('response end: ', body.toString());
    });
    });

    req.on('error', function (e) {
    console.log('problem with request: ' + e.message);
    });

    // write data to request body
    req.write(stringPostData);
    req.end();