NodeJS Strange response from JSON call - json

I got a strage response from my JSON call! This is my response output:
Console Output
And that's my JSON request:
request({
url: url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body)
}
})
I think it's something wrong about the charset, but i don't get how i can change this.
Solution:
// Had to decode url
var encoded = encodeURIComponent(name);
var url = "xxx" + encoded;

I've cleared a lot of misleading junk out of your code to get to the heart of the problem.
This now reproduces the original issue:
var request = require('request');
getItemPrice("StatTrakā„¢ UMP-45 | Delusion (Minimal Wear)")
function getItemPrice(market_hash_name, callback) {
var url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=" + market_hash_name;
request({
url: url,
json: true
}, function(error, response, body) {
console.log(body);
if (!error && response.statusCode === 200) {
console.log(parseFloat(((body.lowest_price).replace(/\,/g, '.')).split('&')[0]));
}
})
}
Your problem is that what you are calling is not a valid URL and the server is reacting to it badly.
You must encode special characters into the URL format:
var url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=" +
encodeURIComponent(market_hash_name);

What kind of REST API do you call? If it's node js API, you can set the charset there
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);
});
});
And if that is not the case, set the request charset like this
request.setHeader('Accept-Charset', 'UTF-8)

Related

Need help in expect conditions, as I need to save JSON and check the length of the object in request call

I'm working on Protractor automation, where I need to save JSON after few activities in browser. Then I'm checking the length of the object but I have seen that no console or expect conditions are working when given in setTimeout.
The console and expect statements are working if not given in setTimeout but the JSON response is asynchronous even before the activities.
test_speck.js :
it('Verify the json', function () {
this.commonfunc.checkJSON();
}
common_functions.js :
checkJSON: function(){
setTimeout(function(){
var request = require('request');
request('https://jsonplaceholder.typicode.com/users', function (error, response, body){
if (!error && response.statusCode == 200){
var importedJSON = JSON.parse(body);
console.log('Length of importedJSON : ' + importedJSON.length);
expect(importedJSON.length).toBeGreaterThan(0);
}
}
},90000);
}
Actual : The spec is being passed without any console.log or expect statements.
Expected : Console and expect statments to be worked
Pass a callback and it should work.
describe('SO Question 58726564', () => {
it('Verify the json', (done) => {
var request = require('request');
request('https://jsonplaceholder.typicode.com/users', function (error, response, body) {
if (!error && response.statusCode == 200) {
var importedJSON = JSON.parse(body);
console.log('Length of importedJSON : ' + importedJSON.length);
expect(importedJSON.length).toBeGreaterThan(10);
}
done()
})
})
})

Get json with headers by using nodejs

How can I get objects from JSON with headers and send an HTTP get a request to a link?
setHeader('header1', 'header1_value') for http://getsomejson.com/json.php
and retrieve data then send them to
randomlinktosenddata.com
You could use a library like request
try this code:
var request = require('request');
var options = {
url: 'http://getsomejson.com/json.php ',
headers: {
'Some-header': 'header-value'
}
};
request(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
// use data
request({url:'randomlinktosenddata.com'} , (error,response,body) => {
// do more stuff...
});
}
});
you can install the request library by doing npm install request
Check this link: https://github.com/request/request
hope this helps

Looping a request that returns results in order they were put in

This code is the same structure as my code:
for (var i in UserNameArray)
{
var Urls = "https://some online api"+UserNameArray[i]+"api key";
//the url changes by plugging in the next array value every iteration.
request({
url: Urls,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
})
}
It returns a json from the URL correctly, but it's printing it out by which every iterated request returns first. How can I change my code so that it prints out the requests in the order that they were requested? This is important as I will have to traverse the specific JSON with a specific value from UserNameArray.
Try this one.
Im going trough all of them, and storing the response in an result array (by index). Then when all of them have finished it goes to a final callback where we print them (in order) to the console.
var async = require('async');
var result = [];
async.eachOf(UserNameArray, function(name, i, cb) {
//the url changes by plugging in the next array value every iteration.
var Urls = "https://some online api"+UserNameArray[i]+"api key";
request({
url: Urls,
json: true
}, function (error, response, body) {
// checking if something went wrong..
if (error) return cb(error);
if (!error && response.statusCode === 200) {
// storing the response to our result array
result[i] = body;
// do a callback, telling that we are done here
cb();
}
})
}, function(err) {
// all done.
// lets print the errors if something went wrong
if (err) console.error(err.message);
// lets print our results
for(var i=0; i<result.length; i++) {
console.log(result[i]);
}
});
Try this before that install async using npm
var async = require('async');
async.forEach(UserNameArray, function (i, cb) {
var Urls = "https://some online api"+i+"apikey";
request({
url: Urls,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
}
cb();
})
});

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);
});
}