L1vet L1vet - 1 month ago 8
Javascript Question

How to use the node.js 'request' library with this http request?

I was trying to make a simple request to site. it should get html text, but it gets ' '

NPM module here: github.com/request/request

Code:

var fs = require('fs');
var request = require('request');

var options = {
url:'https://sample.site/phpLoaders/getInventory/getInventory.php',
encoding : 'utf8',
gzip : true,
forever: true,
headers: {
'Host': 'sample.site',
'Connection': 'keep-alive',
'Content-Length': '58',
'Cache-Control': 'max-age=0',
'Accept': '*/*',
'Origin': 'https://csgosell.com',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'https://sample.site/',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Cookie': 'my-cookies from browser'


},
form: {
stage:'bot',
steamId:76561198284997423,
hasBonus:false,
coins:0
}
};



request.post(options,
function(error, response, body){
console.log(response.statusCode);
if (!error) {
fs.writeFileSync('site.html', body);
}
else{
console.log(error);
}
}
);


Chrome request: https://i.stack.imgur.com/zKQo5.png
Nodejs request:https://i.stack.imgur.com/yH9U3.png

the difference is in headers:
:authority:csgosell.com
:method:POST :path:/phpLoaders/getInventory/getInventory.php :scheme:https

after some googling, I anderstood that it is http2, and tried to put it inow another agent's options, but nothing changed.

var spdy = require('spdy');

var agent = spdy.createAgent({
host: 'sample.site',
port: 443,
spdy: {
ssl: true,
}


}).once('error', function (err) {
this.emit(err);
});

options.agent = agent;

Answer

To answer your question i will copy/paste a part of my code that enable you to receive a post request from your frontend application(angularJS) to your backend application (NodeJS), and another function that enable you to do the inverse send a post request from nodeJS to another application (that might consume it):

1) receive a request send from angularJS or whatever inside your nodeJS app

 //Import the necessary libraries/declare the necessary objects
var express = require("express");
var myParser = require("body-parser");
var app = express();
// we will need the following imports for the inverse operation
var https = require('https')
var querystring = require('querystring')

      // we need these variables for the post request:


      var Vorname ;
      var Name ;
      var e_mail ;
      var Strasse ;


  app.use(myParser.urlencoded({extended : true}));
  // the post request is send from http://localhost:8080/yourpath
  app.post("/yourpath", function(request, response ) {
      // test the post request
      if (!request.body) return res.sendStatus(400);

      // fill the variables with the user data
       Vorname =request.body.Vorname;
       Name =request.body.Name;
       e_mail =request.body.e_mail;
       Strasse =request.body.Strasse;      
      response.status(200).send(request.body.title);

});

2) Do the inverse send a POST request from a nodeJS application to another application

    function sendPostRequest()
    {
        // prepare the data that we are going to send to anymotion  
        var jsonData = querystring.stringify({
                "Land": "Land",
                "Vorname": "Vorname",
                "Name": "Name",
                "Strasse": Strasse,
            });
        var post_options = {
            host: 'achref.gassoumi.de',
            port: '443',
            method: 'POST',
            path: '/api/mAPI',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': jsonData.length
            }
        };
        // request object
            var post_req = https.request(post_options, function(res) {
            var result = '';
            res.on('data', function (chunk) {
                result += chunk;
                console.log(result);
            });
            res.on('end', function () {
            // show the result in the console : the thrown result in response of our post request
            console.log(result);
        });
        res.on('error', function (err) {
            // show possible error while receiving the result of our post request
            console.log(err);
        })
        });
        post_req.on('error', function (err) {
            // show error if the post request is not succeed
            console.log(err);
        });
        // post the data
        post_req.write(jsonData);
        post_req.end(); 
// ps : I used a https post request , you could use http if you want but you have to change the imported library and some stuffs in the code
    }

So finally , I hope this answer will helps anyone who is looking on how to get a post request in node JS and how to send a Post request from nodeJS application.

For further details about how to receive a post request please read the npm documentation for body-parser library : npm official website documentation

Comments