How do I make a https post in Node Js without any third party module?

node.jsPostHttps

node.js Problem Overview


I'm working on a project that requires https get and post methods. I've got a short https.get function working here...

const https = require("https");

function get(url, callback) {
    "use-strict";
    https.get(url, function (result) {
        var dataQueue = "";    
        result.on("data", function (dataBuffer) {
            dataQueue += dataBuffer;
        });
        result.on("end", function () {
            callback(dataQueue);
        });
    });
}

get("https://example.com/method", function (data) {
    // do something with data
});

My problem is that there's no https.post and I've already tried the http solution here with https module https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request-in-node-js but returns console errors.

I've had no problem using get and post with Ajax in my browser to the same api. I can use https.get to send query information but I don't think this would be the correct way and I don't think it will work sending files later if I decide to expand.

Is there a small example, with the minimum requirements, to make a https.request what would be a https.post if there was one? I don't want to use npm modules.

node.js Solutions


Solution 1 - node.js

For example, like this:

const https = require('https');

var postData = JSON.stringify({
    'msg' : 'Hello World!'
});

var options = {
  hostname: 'posttestserver.com',
  port: 443,
  path: '/post.php',
  method: 'POST',
  headers: {
       'Content-Type': 'application/x-www-form-urlencoded',
       'Content-Length': postData.length
     }
};

var req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (e) => {
  console.error(e);
});

req.write(postData);
req.end();

Solution 2 - node.js

Here's a version slightly different from the accepted answer:

  • async
  • You can pass the URL directly (no need to split to hostname, path, port)
  • It handles error HTTP status codes
  • It handles connection timeouts
  • For an alternative content type example, it sends JSON instead of x-www-form-urlencoded
const https = require('https')

async function post(url, data) {
  const dataString = JSON.stringify(data)

  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': dataString.length,
    },
    timeout: 1000, // in ms
  }

  return new Promise((resolve, reject) => {
    const req = https.request(url, options, (res) => {
      if (res.statusCode < 200 || res.statusCode > 299) {
        return reject(new Error(`HTTP status code ${res.statusCode}`))
      }

      const body = []
      res.on('data', (chunk) => body.push(chunk))
      res.on('end', () => {
        const resString = Buffer.concat(body).toString()
        resolve(resString)
      })
    })

    req.on('error', (err) => {
      reject(err)
    })

    req.on('timeout', () => {
      req.destroy()
      reject(new Error('Request time out'))
    })

    req.write(dataString)
    req.end()
  })
}

const res = await post('https://...', data)

Solution 3 - node.js

In Node.js 18

Say goodbye to the node-fetch package ,axios and request ,... now the fetch API is available on the global scope by default.

const res = await fetch('https://nodejs.org/api/documentation.json');
if (res.ok) {
  const data = await res.json();
  console.log(data);
}

We can make requests as we do in browsers.

For More Information

Solution 4 - node.js

Thank goodness, node-fetch is here,

everything else is ancient history.

const fetch = require('node-fetch');
// note: use npm install [email protected] to be able to use "require"

console.log("trying ...")

let body = {
    "ids": ["4e4e4e4e-4e4e-4e4e-4e4e-4e4e4e4e4e4e"]
};

fetch('https://blahblah.com/blah', {
    method: 'POST',
    body: JSON.stringify(body),
    headers: {
        'accept': 'application/json',
        'x-api-key': 'superamazingsecretcryptostuff',
        'Content-Type': 'application/json'
        // fyi, NO need for content length
    }
})
    .then(res => res.json())
    .then(json => console.log(json))
    .catch (err => console.log(err))

console.log("done....")

Job done.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNovaView Question on Stackoverflow
Solution 1 - node.jsaringView Answer on Stackoverflow
Solution 2 - node.jsMax IvanovView Answer on Stackoverflow
Solution 3 - node.jsAbolfazlRView Answer on Stackoverflow
Solution 4 - node.jsFattieView Answer on Stackoverflow