JavaScript
Node.js: Native requests vs. request package

by Benny Neugebauer

var qs = require("querystring");
var http = require("https");
 
var options = {
  "method": "POST",
  "hostname": "api.uptimerobot.com",
  "port": null,
  "path": "/v2/newMonitor",
  "headers": {
    "cache-control": "no-cache",
    "content-type": "application/x-www-form-urlencoded"
  }
};
 
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(body.toString());
  });
});
 
req.write(qs.stringify({ api_key: 'enterYourAPIKeyHere',
  format: 'json',
  type: '1',
  url: 'http://myMonitorURL.com',
  friendly_name: 'My Monitor' }));
req.end();

var request = require("request");
 
var options = { method: 'POST',
  url: 'https://api.uptimerobot.com/v2/newMonitor',
  headers:
   { 'content-type': 'application/x-www-form-urlencoded',
     'cache-control': 'no-cache' },
  form:
   { api_key: 'enterYourAPIKeyHere',
     format: 'json',
     type: '1',
     url: 'http://myMonitorURL.com',
     friendly_name: 'My Monitor' } };
 
request(options, function (error, response, body) {
  if (error) throw new Error(error);
 
  console.log(body);
});