Yan Li Yan Li - 2 years ago 110
Node.js Question

use node.js send request for baidu map API get wrong response?

Im trying to use node.js to send http requst and call baidu map API.
my code in blow:

If you past the url and use browser directly, it will give right response in right format.

But when I use node to send request, I get problem.

var request = require('request');
{ method: 'GET',
uri: 'http://api.map.baidu.com/place/v2/suggestion?query=beijing&region=131&output=json&ak=****hLQKu9ap9fPq5N1ExF1Kk7xe5Eah'
, function (error, response, body) {


Meanwhile, if I change the url contains some Chinese like:


In node.js it will give status code 400 and totally wrong response.

Answer Source

you must encode your uri with encodeURI

uri: encodeURI('http://api.map.baidu.com/place/v2/suggestion?query=北京理工大学&region=北京&output=json&ak=3104hLQKu9ap9fPq5N1ExF1Kk7xe5Eah')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download