Software Engineer at 99X TechnologiesFull Stack Developer expertise in React-nativeInformation Systems graduate.
Sending POST requests from Node.js
January
19th,
2018
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
varhttp=require('http');varexpress=require('express');varrequest=require('request');varapp=express();varserver=http.createServer(app);varport=9000;//Startingtheserverserver.listen(port);console.log('Server Started on port '+port);//POSTrequestrequest.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