admin管理员组文章数量:1345725
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"
}
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"
}
Share
Improve this question
asked Jul 7, 2016 at 20:49
DollarCoffeeDollarCoffee
6141 gold badge5 silver badges13 bronze badges
4
-
You're immediately printing out the
body
variable before you've doneJSON.parse
on it. Why would it be parsed at that point? – Mike Cluck Commented Jul 7, 2016 at 20:51 -
I meant on this line (
console.log(response.statusCode); console.log(body);
). You need to wait until it's been processed in theend
event as peteb said. Either that or remove theend
event entirely. Upon closer inspection, I don't even know why you have that there. You're acting as thoughbody
is being generated by a stream but it shouldn't be. – Mike Cluck Commented Jul 7, 2016 at 21:03 -
Like I said, get rid of those. They're pointless. Remove the
response.on
blocks and try just doingbody = JSON.parse(body); console.log(typeof body);
– Mike Cluck Commented Jul 7, 2016 at 21:08 -
1
If it's saying
object
that means it has been parsed. JSON is a string which is parsed into an object. Then you can access your values by doingbody.name
,body.authorities
, etc. Example – Mike Cluck Commented Jul 7, 2016 at 21:16
2 Answers
Reset to default 5The 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);
The following line doesn't wait for your response to be parsed.
res.send(body);
Remove it and wait to respond from your response.on('end')
event.
Edit to include restructured request
I'd structure your request differently. You're not streaming your response so there shouldn't be much reason to listen for response events. Also you can get rid of your need for JSON.parse()
by letting request handle that for you by indicating that the returning body is JSON.
request({
method: 'GET',
url: authServer + '/uaa/user',
json: true, // indicates the returning data is JSON, no need for JSON.parse()
auth: {
user: null,
password: null,
sendImmediately: true,
bearer: bearerToken
}
}, function (error, response, body) {
if(error){
console.log('ERROR with user request.');
return res.sendStatus(500); // Return back that an error occurred
}
else {
console.log(response.statusCode);
console.log(body);
var text = '';
for (var key in body) {
text += 'Index is: ' + key + '\nDescription is: ' + body[key];
}
return res.status(200).send(text);
}
});
本文标签: javascriptParsing JSON response body in ExpressjsStack Overflow
版权声明:本文标题:javascript - Parsing JSON response body in Express.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743791570a2539666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论