NodeJS request.get body

node js handle post request
nodejs request-promise
nodejs https request
nodejs response body
node js get post data without express
nodejs request json
node js get data from url
nodejs keepalivetimeout

I want to use NodeJS to get the response from 2 urls, and put them in an array for later processing.

I am unable to store the body/resp outside the request.get function. Resp has undefined outside request.get.

Any suggestion what I am doing wrong? Thx

var msurls = ["http://example.org/get1", "http://example.org/get2"];
var request = require("request");
var msresult = [];

msurls.forEach(function(msurl) {
        var resp;
        request.get(msurl,function(error,response,body){
                if(error) {
                        console.log(error);
                } else {
                        resp = body;
                }
        });
        msresult.push(resp);
});
console.log(msresult);

You have an issue with the timing of your code. Your msresult.push(resp); is fired before you request.get has finished, so you are pushing resp that doesn't have a value yet.

I think you can use await as so... (I may be wrong though)

var msurls = ["http://example.org/get1", "http://example.org/get2"];
var request = require("request");
var msresult = [];

msurls.forEach(function(msurl) {
    await request.get(msurl, function(error, response, body){
        if(error) {
            console.log(error);
        } else {
            msresult.push(body);
        }
    });
});
console.log(msresult);

server with a request, Getting at the body data is a little more involved than accessing request headers. The request object that's passed in to a handler implements the [  Get HTTP request body data using Node.js Here is how you can extract the data that was sent as JSON in the request body. If you are using Express, that's quite simple: use the body-parser Node.js module.

Take a look at Promise.all() - it allows you to execute multiple async requests.

Cleaner solution would be:

const request = require("request");

const getData = async (requests) => {
  return Promise.all(requests);
};

const doWork = async () => {
  const msurls = ["http://example.org/get1", "http://example.org/get2"];
  const requests = msurls.map(url => request.get(url));

  const result = await getData(requests);
  console.log(result);
  return result;
};

As for further learning, take a look into ES6 features Node supports and read up about asynchronous programming in Javascript.

request/request: Simplified HTTP request client., request .get('http://google.com/img.png') .on('response', function(response) { console.log(response. The HTTP method, headers, and entity-body data will be sent. Also, util.promisify , which is available from Node.js v8.0 can be used to  @tfmontague: POST requests typically use a response body, not GET. Post request have a body and GET request does not, but a GET response can have a body. – Cyrbil Oct 5 '15 at 10:48 |

request module doesn't support promises, use request-promise-native instead, if not, you have to use callbacks as @Gediminas wrote.

I do prefer this even cleaner solution which is more modern javascript:

let msurls = ["http://example.org", "http://example.org"]
let request = require("request-promise-native")

async function doRequests() {
    return await(Promise.all(
        msurls.map(async msurl => await request(msurl)))
    )
}

doRequests().then(msresult => console.log(msresult))

You already have an array with the results and the code looks synchronous.

The Node.js Request Module, parse(body); console.log(json); });. Using the options object, this request uses the GET method to retrieve JSON data directly from Reddit, which  fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.

Where is body in a nodejs http.get response?, http.request docs contains example how to receive body of the response through handling data event: var options = { host: 'www.google.com',  This post highlights an attempt to take a peek at the raw format of data sent in a POST request body and how one could parse it. There are packages like body-parser for Express that do this for us so this post is merely for learning purposes. I won’t suggest the use of this solution in production. When making post requests to the server via

How to handle the POST request body in Node.js without using a , This post highlights an attempt to take a peek at the raw format of data sent in a POST request body and how one could parse it. There are  In this tutorial, you’ll be taught how to receive GET requests in Node.js the easy way, with the help of Express.js and the body-parser library. The first step: If you haven’t already, cd to your project’s directory (wherever you are going to put your Node.js code) and install Express.js using the Node package manager (NPM) at a command prompt (this applies to both Windows and Linux).

request.Response.body JavaScript and Node.js code examples , tap.test('GET /ping responds with status 200 / "pong"', (t) => { const server = app.​listen() const port = server.address().port const url = `http://localhost:${port}/ping`​  Node.js - Request Object - The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. Home Jobs

Comments