Hello all of you! from this article we are going to explore how to send POST requests from a Node.js Application. Node application using here is very much simple & very easy to understand.

For the easy coding we are using requests npm package to send the requests. In requests npm package you can find many easy http method implementations

  • request.get(): Defaults to method: “GET”.
  • request.post(): Defaults to method: “POST”.
  • request.put(): Defaults to method: “PUT”.
  • request.patch(): Defaults to method: “PATCH”.
  • request.del() / request.delete(): Defaults to method: “DELETE”.
  • request.head(): Defaults to method: “HEAD”.
  • request.options(): Defaults to method: “OPTIONS”.


So lets directly go into the code

var http       = require('http');
var express    = require('express');
var request    = require('request');
var app        = express();

var server = http.createServer(app);
var port = 9000;

//Starting the server
server.listen(port);
console.log('Server Started on port ' + port);

//POST request
request.post(
  'http://localhost:8090/api/users',
  { json: { key1: 'value1', key2: 'value2' } },
  function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body);
    }
  }
);



To run the code snippet

Step 1 - run npm init to initiate a command line questionnaire that will conclude with the creation of a package.json in the directory

$ npm init


Step 2 - You first need to install related npm packages using npm install <package-name> command

$ npm install body-parser
$ npm install express
$ npm install request


Step 3 Run the js file

$ node file-name.js




Omal Perera

Information Systems undergraduate at Sabaragamuwa University of Sri Lanka
Full Stack Developer
Trainee Software Engineer at 99X Technologies.