Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
82
rated 0 times [  89] [ 7]  / answers: 1 / hits: 24274  / 8 Years ago, thu, july 7, 2016, 12:00:00

A Node.js/Express.js app makes a RESTful call to another app and receives JSON in response. But the JSON response is not being parsed into new variables. What specific changes need to be made to the code below, so that the JSON body can be successfully parsed into new variables that the receiving Node.js/Express.js app can use for further processing?



Here is the Node.js/Express.js code which is currently receiving the JSON body response:



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

app.get('/user**', function(req, res) {
console.log(You Hit The User Route TOP);
request.get(authServer + '/uaa/user', function (error, response, body) {
if(error){console.log('ERROR with user request.')}
if (!error){// && response.statusCode == 200) {
console.log(response.statusCode); console.log(body);

response.on('data', function(chunk){
console.log('inside response.on(data...)');
body += chunk;
});

response.on('end', function(){
console.log('inside response.on(end...)');
body = JSON.parse(body);
var text = '';
for (var key in body){
text += 'Index is: ' + key +
'nDescription is: ' + body[key]
}

// The Description is: descriptive string
console.log(Got a response: , text);
res.send(text);
});
res.send(body);
};
}).auth(null, null, true, bearerToken);//this inserts bearer token in the GET request
console.log(You Hit The User Route BOTTOM);
});


Here are the nodemon logs for the GET shown in the code. Note that the response.on() blocks are never called because their SYSO never prints:



You Hit The User Route TOP
You Hit The User Route BOTTOM
200
{ long JSON string, which is formatted and truncated below for easier reading }
GET /user 200 182.862 ms - 1296


And here is the formatted and truncated JSON body, which illustrates the format of the data that needs to be parsed into Node.js/Express.js JavaScript variables:



{
details:
{
remoteAddress:127.0.0.1,
sessionId:null,
tokenValue:SomeLongTokenString,
tokenType:Bearer,
decodedDetails:null
},
authenticated:true,
userAuthentication:
{
details:null,
authorities:
[
{
authority:ROLE_ADMIN
},
{
authority:ROLE_USER
}
],
authenticated:true,
principal:user,
credentials:N/A,
name:user
},
name:user
}

More From » json

 Answers
17

The problem is you're acting as though response is a stream that's incrementally giving you the JSON but you've already proven to yourself that's not true with your first console.log(body) statement. Instead, you can parse body immediately and begin working on it. You can also simplify your request handler.



if (error) {
console.log('ERROR with user request.')
return res.sendStatus(500);
}

body = JSON.parse(body);
var text = '';
for (var key in body) {
text += 'Index is: ' + key + 'nDescription is: ' + body[key]
}
// The Description is: descriptive string
console.log(Got a response: , text);
res.send(text);

[#61470] Wednesday, July 6, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaileya

Total Points: 168
Total Questions: 95
Total Answers: 72

Location: Antigua and Barbuda
Member since Sat, Apr 24, 2021
3 Years ago
;