q-io: how to read response.body JSON object using Promises?

charliebrownie

I need to make an http request from one server to another server.

The server that will handle this request will just return a JSON Array of objects:

app.get('/array', function (req, res) {
  console.log('GET request received!');
  var response_array = [
    {
      value: 1234
    },
    {
      value: 1234
    },
    {
      value: 55
    }
  ];

  res.send(response_array);
});

I am using q-io to send the http request to that GET method /array and obtain the array:

var _getArray = function(externalUrl) {
  var request = {
    method: "GET",
    url: externalUrl
  };

  return HTTP.request(request)
    .then(function(response) {
      // I need to return the Array inside the body (a Promise) to
      // iterate on it later
      return response.body.read(); // but .read() returns a Buffer
    });
}

Both servers work right, as the request is correctly sent and received from one to another, and also the response.

The issue I have is that I am not achieving to obtain the JSON Array - as read() returns a Buffer and just response.body doesn't return the Array (as the docs say, it returns a representation of a readable stream)... how can I handle this properly to obtain the Array correctly with Promises?

Bergi

If you can get a promise for the body content of the response, you can get a promise for the JSON payload from that by passing it through JSON.parse:

return HTTP.request(request)
  .then(function(response) { return response.body.read() })
  .then(JSON.parse);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read JSON request body using Dropwizard

From Dev

How to synchronously execute FOR LOOP using $q promises?

From Dev

$q promises - Object is not a function

From Dev

Nesting promises with q-io

From Dev

Polling with promises using Q

From Dev

How to read JSON response?

From Dev

how to use q promises?

From Dev

Golang: how to read response body of ReverseProxy?

From Dev

How to read a JSON response using HttpClient in vb.net

From Dev

How do I read a streaming response body using Golang's net/http package?

From Dev

Using Q/promises vs callbacks

From Dev

How to read a json response from a akka-http response entity using json4s

From Dev

How to post a JSON object as body?

From Dev

How to read a JSON response in Unity?

From Dev

How to check JSON in response body with mockMvc

From Dev

How to customize the response body JSON conversion in Retrofit?

From Dev

Read JSON response object in php/js

From Dev

Read JSON response object in php/js

From Dev

How to use Q promises in ExpressJS?

From Dev

How to sequentially call promises with $q

From Dev

npm request not getting json response when doing body.{object}

From Dev

Double Object Nesting JSON Response Body [Objective C]

From Java

FastAPI: how to read body as any valid json?

From Dev

How to read JSON body in HttpActionExecutedContext in case of exception

From Dev

Chained Promises (Q deferred) with Object / Prototype

From Java

How to read ASP.NET Core Response.Body?

From Dev

How to read the HTTP response body in catch exception strategy

From Dev

Read Json Object Using PHP

From Dev

$q.all(promises)and structure of promises object to collect the returned data

Related Related

HotTag

Archive