How to send a POST request from node.js Express?
var request = require('request'); function updateClient(postData){ var clientServerOptions = { uri: 'http://'+clientHost+''+clientContext, body: JSON.stringify(postData), method: 'POST', headers: { 'Content-Type': 'application/json' } } request(clientServerOptions, function (error, response) { console.log(error,response.body); return; }); }
For this to work, your server must be something like:
var express = require('express');var bodyParser = require('body-parser');var app = express();app.use(bodyParser.urlencoded({ extended: false }));app.use(bodyParser.json())var port = 9000;app.post('/sample/put/data', function(req, res) { console.log('receiving data ...'); console.log('body is ',req.body); res.send(req.body);});// start the serverapp.listen(port);console.log('Server started! At http://localhost:' + port);
As described here for a post request :
var http = require('http');var options = { host: 'www.host.com', path: '/', port: '80', method: 'POST'};callback = function(response) { var str = '' response.on('data', function (chunk) { str += chunk; }); response.on('end', function () { console.log(str); });}var req = http.request(options, callback);//This is the data we are posting, it needs to be a string or a bufferreq.write("data");req.end();
you can try like this:
var request = require('request');request.post({ headers: {'content-type' : 'application/json'} , url: <your URL>, body: <req_body in json> } , function(error, response, body){ console.log(body); });