JSON parse error in Node.js - json

In Node v0.4.12,
var http = require('http');
var options = {
host: 'example.com',
port: 80,
path: 'example.aspx'
};
var req = http.request(options, function(res) {
var result = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
result += chunk;
});
res.on('end', function () {
var jsonStr = JSON.stringify(result);
var data = JSON.parse(jsonStr);
console.log(data['Date']);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
When I tried console.log(data) it works but console.log(data['Date']) returns undefined.
Example JSON scheme:
{"Date":"17.03.2012 15:28:47", "Categories":[{"ID":1,"Name":"Foo","URLSlug":"foo"}]}
How can I fix this problem?
Thanks!

If I remove all of the http request code and just attempt to parse and print out the Date part of the parsed json:
$ cat ex2.js
var data = JSON.parse('{"Date":"17.03.2012 15:28:47", "Categories":[{"ID":1,"Name":"Foo","URLSlug":"foo"}]}');
console.dir(data);
console.log(data['Date']);
$ node ex2.js
{ Date: '17.03.2012 15:28:47',
Categories: [ { ID: 1, Name: 'Foo', URLSlug: 'foo' } ] }
17.03.2012 15:28:47
$
The example code works fine. Are you running the literal code you posted? If you are, then the error is probably that example.com isn't returning the JSON you've given as an example for example.asp. For me that returns:
$ curl -I http://example.com/example.asp
HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: Keep-Alive
Content-Length: 0
Which is not JSON, just a 302 Redirect response. You might want to print out the full result so you can see if it's actually JSON or something else (in this case, HTML):
$ cat example.js
var http = require('http');
var options = {
host: 'google.com',
port: 80,
path: '/'
};
var req = http.request(options, function(res) {
var result = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('got data back!');
result += chunk;
});
res.on('end', function () {
console.log('request is done');
console.dir(result);
var jsonStr = JSON.stringify(result);
var data = JSON.parse(jsonStr);
console.log(data['Date']);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
For me that gives:
$ node example.js
got data back!
request is done
'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\nhere.\r\n</BODY></HTML>\r\n'
undefined
HTH,
Kyle

Related

Nodejs getting a incomplete response body

I am using http module to send a request to api. So my response body is very large, and I am getting incomplete and when trying to parse to javascript object I am getting an error, that the json is not valid.
Here is my code.
function sendPostRequest(method, url, data, callback) {
if (typeof data === 'undefined') {
data = {};
}
var data = querystring.stringify(data);
var post_options = {
host: API.Host,
port: API.Port,
path: API.Prefix + url,
method: method,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + API_USER.token
}
};
var post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
callback(chunk);
});
});
// post the data
post_req.write(data);
post_req.end();
}
sendPostRequest('GET', 'user/get_accounts', data, function (res) {
res = JSON.parse(res);
mainWindow.webContents.send('user:account', res);
return;
}, true);
Please help to solve this problem! Thanks!
If the data is large and it's provided in chunks(incomplete json) you might have better luck with:
var post_req = http.request(post_options, function (res) {
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
callback(rawData);
});
});

Extracting a JSON Object from a URL

We are attempting to extract a JSON Object from a URL through http requesting. However, when we consistently getting the "undefined" when we try to return the text. Is there a problem in the way that we are implementing the http request?
function getUserData(email) {
var pathURL = "/" + email + "/data"
var options = {
host: 'localhost',
port: 3000,
path: pathURL,
method: 'GET',
headers: {
accept: 'application/json'
}
};
var x = http.request(options, function(res){
console.log("Connected");
res.on('data', function(data){
console.log(data);
});
});
}
Close the http.request() by using
x.end();
Here a reference to a similar question.
Sending http request in node.js
Try logging error as:
req.on('error', function(err){
console.log('problem with request:',err.message);
});
Meanwhile check the documentation of http library as well.
The response body are data but not returning to x.
var body = []
request.on('data', function(chunk) {
body.push(chunk)
}).on('end', function() {
body = Buffer.concat(body).toString()
// all is done, you can now use body here
})

cors unexpected end of JSON input

I am parsing my json on end but I am still receiving this error.
'use strict';
const http = require('http');
const tools = require('./tools.js');
const server = http.createServer(function(request, response) {
console.log("received " + request.method + " request from " + request.headers.referer)
var body = "";
request.on('error', function(err) {
console.log(err);
}).on('data', function(chunk) {
body += chunk;
}).on('end', function() {
console.log("body " + body);
var data = JSON.parse(body); // trying to parse the json
handleData(data);
});
tools.setHeaders(response);
response.write('message for me');
response.end();
});
server.listen(8569, "192.168.0.14");
console.log('Server running at 192.168.0.14 on port ' + 8569);
Data being sent from the client:
var data = JSON.stringify({
operation: "shutdown",
timeout: 120
});
I successfully receive the json but I am unable to parse it.
Update:
I've updated the code to include the server code in its entirety.
To be perfectly clear, using the following code:
....
}).on('end', function() {
console.log("body " + body);
var json = JSON.parse(body); // trying to parse the json
handleData(json);
});
I get this:
However, this:
....
}).on('end', function() {
console.log("body " + body);
//var json = JSON.parse(body); // trying to parse the json
//handleData(json);
});
produces this
Can we see the server code, please?
Here is a working end-to-end example which is (more or less) what you are attempting, I believe.
"use strict";
const http = require('http');
/********************
Server Code
********************/
let data = {
operation: 'shutdown',
timeout: 120
};
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(data));
res.end();
});
server.listen(8888);
/********************
Client Code
********************/
let options = {
hostname: 'localhost',
port: 8888,
path: '/',
method: 'POST',
headers: {
'Accept': 'application/json'
}
};
let req = http.request(options, res => {
let buffer = '';
res.on('data', chunk => {
buffer += chunk;
});
res.on('end', () => {
let obj = JSON.parse(buffer);
console.log(obj);
// do whatever else with obj
});
});
req.on('error', err => {
console.error('Error with request:', err);
});
req.end(); // send the request.
It turns out that as this is a cross-origin(cors) request, it was trying to parse the data sent in the preflighted request.
I simply had to add an if to catch this
....
}).on('end', function() {
if (request.method !== 'OPTIONS') {
var data = JSON.parse(body);
handleData(data);
}
});
Further reading if you're interested: HTTP access control (CORS)
Put the identifiers in quotes.
{
"operation": "shutdown",
"timeout": 120
}
http://jsonlint.com/ Is a helpful resource.

node.js http.request for json, undefined in front of the json

I'M trying to get data from embed.ly via node.js.
Everything looks ok but it puts an "undefined" in front of the data:
Maybe it has something to do with setEncoding('utf8) ?
The results looks like this:
undefined[{ validjson }]
The function:
function loadDataFromEmbedLy( params, queue ){
try {
var body;
var options = {
host: 'api.embed.ly',
port: 80,
path: '/1/oembed?wmode=opaque&key=key&urls='+params,
method: 'GET',
headers: {'user-agent': ''}
};
var req = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('end', function() {
if( typeof body != 'undefined' ){
console.log( body );
}
});
res.on('data', function ( chunk ) {
if( typeof chunk != 'undefined' ){
body += chunk;
}
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
} catch(e) { console.log("error " + e); }
}
It's because body is initially undefined. When you append to it using +=, it will append it to the string "undefined". I hope that makes sense.
Solution: declare body as the empty string: var body = "";
Second: I really recommend checking out Mikeal Rogers' request.
Edit: request is a little easier than the basic http api. Your example:
function loadDataFromEmbedLy (params) {
var options = {
url: 'http://api.embed.ly/1/oembed',
qs: {
wmode: 'opaque',
urls: params
},
json: true
};
request(options, function (err, res, body) {
console.log(body);
});
}

how to get session from codeigniter with node.js

i'm writing application like social network where in my application can show status update and chat . when i search on internet i found node.js for long polling technology and i think i can use that for chat and streaming page in my application. but when i use node.js i have a stack
this is a technology i want to my project:
1) i'm using codeigniter for framework and mysql database in address localhost:81/myproject
2) and using node.js in port 127.0.0.1:8080 to chat and streaming page
this is code javascript server with node.js name is server.js
var sys = require("sys"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
events = require("events");
function load_static_file(uri, response) {
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
}
var local_client = http.createClient(81, "localhost");
var local_emitter = new events.EventEmitter();
function get_users() {
var request = local_client.request("GET", "/myproject/getUser", {"host": "localhost"});
request.addListener("response", function(response) {
var body = "";
response.addListener("data", function(data) {
body += data;
});
response.addListener("end", function() {
var users = JSON.parse(body);
if(users.length > 0) {
local_emitter.emit("users", users);
}
});
});
request.end();
}
setInterval(get_users, 5000);
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
if(uri === "/stream") {
var listener = local_emitter.addListener("users", function(users) {
response.writeHead(200, { "Content-Type" : "text/plain" });
response.write(JSON.stringify(users));
response.end();
clearTimeout(timeout);
});
var timeout = setTimeout(function() {
response.writeHead(200, { "Content-Type" : "text/plain" });
response.write(JSON.stringify([]));
response.end();
local_emitter.removeListener(listener);
}, 10000);
}
else {
load_static_file(uri, response);
}
}).listen(8383);
sys.puts("Server running at http://localhost:8383/");
now in codeigniter side i making webservices on url http://localhost:81/myproject/getUser with response is json format and i access this with session auhtentication if not is redirect to login page.
[{"user_id":"2a20f5b923ffaea7927303449b8e76daee7b9b771316488679","token":"3m5biVJMjkCNDk79pGSo","username":"rakhacs","password":"*******","name_first":"rakha","name_middle":"","name_last":"cs","email_id":"email#gmail.com","picture":"img\/default.png","active":"1","online":"0","created_at":"2011-09-20 11:14:43","access":"2","identifier":"ae70c3b56df19a303a7693cdc265f743af5b0a6e"},{"user_id":"9a6e55977e906873830018d95c31d2bf664c2f211316493932","token":"RoUvvPyVt7bGaFhiMVmj","username":"ferdian","password":"*****","name_first":"willy","name_middle":"","name_last":";f;w","email_id":"email1#gmail.com","picture":"img\/default.png","active":"1","online":"0","created_at":"2011-09-20 11:47:20","access":"2","identifier":"1ccd4193fa6b56b96b3889e59c5205cc531177c9"}]
this is the point problem when i execute node server.js
i get error like this undefined:0
sysntaxerror:unexpected end of input
at object.parse(native)
i don't know about that error i think because i using session ? but i'm not sure.
when i test using test.js for testing my webservices
var http = require("http")
var options = {
host: 'localhost',
port: 81,
path: '/myproject/getUser',
method: 'GET'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
and this is the output
problem with request: parse error
but when i using another webservice who has been response json format too and not using session that's work i can get the body response..if this problem is session how i can fix this?..this is can make me confused..thanks for your comment
hard to say but I would primary try to send an object in jSON and not directly an array. i.e. just encapsulate the array into an object:
{"data":[…]}
This "may" be causing the parse error.
Something else, for this purpose, it won't be bad to add the following to your responding PHP method:
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');