Search code examples
node.jspostdata

How do I do a `POST` with `Node.js`?


I've followed examples and samples and everything and I am struggling.. the equivalent PHP code I'm using is:

$url = json_encode($userArray);

$postURL = "https://mysite.com/v4/bulk?api_key=51076b4234e62c7b4ef8e33717a3bce5";
$ch = curl_init($postURL);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ( 'Content-Type: application/json'));
curl_setopt ($ch, CURLOPT_POSTFIELDS,$url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$result = json_decode($result, true);

I just can't get this to work within Node. Any help would be greatly appreciated.

Thanks.


Solution

  • request

    var request = require("request");
    
    request({
      url: "https://...",
      method: "POST",
      json: userArray
    }, function _callback(err, res, body) {
      var result = body;
    });