admin管理员组

文章数量:1404922

I've built a small script that allow me to get keywords suggestion from Google search API.

The main problem is if the response contain special characters (like à é ù etc.) : my application return me unreadable keywords like that : �,�a,�a va,� majuscule,�a marche,�,�a y est,�a film,�gag,�a il est revenu,�a va de soi,,[object Object]

Here's my Node.js Script :

var express = require('express');
var request = require('request');
var app = express();
app.get('/sug', function (req, res) {
    var KW = req.query.KW ;
    console.time("Délai");
    var url = ";q=" + KW + "&json=t&client=hp";
    request(url, function (err, resp, body) {
        body = JSON.parse(body);
        res.end(body.toString());
        console.timeEnd("Délai");
    });
});
app.listen(1337);
console.log('Serveur : OK');

The call is easy to make, just type http://localhost:1337/sug?KW=ç in your browser.

Do you know how to solve this and get the utf-8 working ?

I've built a small script that allow me to get keywords suggestion from Google search API.

The main problem is if the response contain special characters (like à é ù etc.) : my application return me unreadable keywords like that : �,�a,�a va,� majuscule,�a marche,�,�a y est,�a film,�gag,�a il est revenu,�a va de soi,,[object Object]

Here's my Node.js Script :

var express = require('express');
var request = require('request');
var app = express();
app.get('/sug', function (req, res) {
    var KW = req.query.KW ;
    console.time("Délai");
    var url = "http://clients1.google.fr/plete/search?hl=fr&q=" + KW + "&json=t&client=hp";
    request(url, function (err, resp, body) {
        body = JSON.parse(body);
        res.end(body.toString());
        console.timeEnd("Délai");
    });
});
app.listen(1337);
console.log('Serveur : OK');

The call is easy to make, just type http://localhost:1337/sug?KW=ç in your browser.

Do you know how to solve this and get the utf-8 working ?

Share Improve this question edited Dec 21, 2015 at 12:12 Arnaud Boyer asked Dec 21, 2015 at 11:23 Arnaud BoyerArnaud Boyer 231 gold badge1 silver badge4 bronze badges 5
  • The URL you posted is a network local URL, and therefore not accessible for anyone not on your network (i.e. us). – Tiddo Commented Dec 21, 2015 at 11:34
  • 1 problem with clients1.google.fr response. It's encoded with "ISO-8859-1" and it returns ["ç",["�","�a","� encoding","�in takvimi","�eyrek alt�n fiyat�","��plak k�zlar","�a��atay ulusoy","��plak kad�nlar","�ukurova �1\u20444niversitesi","�ocuk nas�l yap�l�r"],[],{"google:suggesttype":["QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY","QUERY"]}] But after convert into UTF-8 we get that strange symbols ¿½,�a – vmk Commented Dec 21, 2015 at 12:37
  • @vmk So why this work in this URL : clients1.google.fr/plete/… ?? – Arnaud Boyer Commented Dec 21, 2015 at 12:41
  • 1 @Tiddo Yes, you must to install node.js and run my script to see it on your browser ;) – Arnaud Boyer Commented Dec 21, 2015 at 12:42
  • Because your browser is honouring the Content-Type HTTP response header and is interpreting the encoding correctly. – deceze Commented Dec 21, 2015 at 12:42
Add a ment  | 

3 Answers 3

Reset to default 1

Like vmk said, it's because the response is using ISO-8859-1, you have to manually convert to utf-8. iconv package can help you with that:

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

var KW = 'ç' ;
console.time("Délai");
var url = "http://clients1.google.fr/plete/search?hl=fr&q=" + KW + "&json=t&client=hp";

var options = {
    url: url,
    encoding: null // << set encoding to null so request don't try to force utf-8
};

var ic = new iconv.Iconv('iso-8859-1', 'utf-8');

request(options, function (err, resp, body) {
    // body is a Buffer not a string, convert to utf-8 buffer then to utf-8 string
    body = ic.convert(body).toString('utf-8');
    console.log(body);
    console.timeEnd("Délai");
});

Thanks for all the help. So i did this code using your feedbacks :

var express = require('express');
var request = require('request');
var iconv = require('iconv');
var app = express();
app.listen(1337);
console.log('Serveur : OK');
app.get('/sug', function (req, res) {
    var KW = req.query.KW;
    console.time("Délai");
    var url = "http://clients1.google.fr/plete/search?hl=fr&q=" + KW + "&json=t&client=hp";
    var options = {
        url: url,
        encoding: null // << set encoding to null so request don't try to force utf-8
    };
    var ic = new iconv.Iconv('iso-8859-1', 'utf-8');
    request(options, function (err, resp, body) {
        res.set({ 'content-type': 'application/json; charset=utf-8' });
        body = ic.convert(body).toString('utf-8');
        body = JSON.parse(body);
        res.end(body.toString());
        console.timeEnd("Délai");
    });
});

It work great !

http://localhost:1337/sug?KW=%C3%A7a

ça,ça,ça va,ça marche,ça y est,ça film,ça il est revenu,ça va de soi,ça te va,ça s'est bien passé,ça m'énerve,,[object Object]

response set
response.set({ 'content-type': 'application/json; charset=utf-8' });

app.use('/reverse',function (requsting,response) { request.get({ url: url, json: true, headers: {'User-Agent': 'request'} }, (err, res, data) => { if (err) { console.log('Error:', err); } else if (res.statusCode !== 200) {

        response.end(JSON.stringify({status : 'error'}));

    } else {
        response.set({ 'content-type': 'application/json; charset=utf-8' });

        response.end(JSON.stringify({status : 'ok','api' : 'website : https://homeandroid.ir','json':data}));

    }
});

});

本文标签: javascriptNodeJS JSONparse UTF8 issueStack Overflow