NodeJS Strange response from JSON call
I got a response from my JSON call. This is my answer: 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 there is something wrong in the charset, but I don't understand how to change this.
Decision:
// Had to decode url
var encoded = encodeURIComponent(name);
var url = "xxx" + encoded;
+3
source to share
2 answers
I've cleaned up a lot of erroneous garbage from your code to get the root of the problem.
This now reproduces the original problem:
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 not responding well to it.
You have to URL-encode special characters:
var url = "http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=" +
encodeURIComponent(market_hash_name);
+1
source to share
Which REST API are you calling? If it is node js API you can set encoding 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 it is not, set the request charset like this
request.setHeader('Accept-Charset', 'UTF-8)
-1
source to share