http request json乱码
发布于 8 年前 作者 bwpAj 4270 次浏览 来自 问答

postData = ‘{“uname”:“15078381887”,“village_num”:“0003006”,“page”:“1”}’; var options = { hostname: ‘10.20.100.55’, port: ‘’, path: ‘xxxxxxxxxxxxxxxxxxxxxxxxxx’, method: ‘POST’, encoding: null, headers: { ‘User-Agent’: ‘Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36’, ‘Accept’: ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8’, ‘Content-Type’: ‘application/json; charset=utf-8’, ‘Accept-Encoding’: ‘gzip, deflate’, ‘Accept-Language’: ‘zh-CN,zh;q=0.8’, ‘Cache-Control’: ‘no-cache’, ‘Connection’: ‘keep-alive’, ‘Host’: ‘10.20.101.43’, ‘Origin’: ‘http://10.20.101.43’, ‘Content-Length’: Buffer.byteLength(postData,‘utf8’) } };

var req = http.request(options, function(res){ console.log(‘STATUS:’ + res.statusCode); console.log('HEADERS: ’ + JSON.stringify(res.headers)); res.setEncoding(‘utf8’); var responseString = []; res.on(‘data’,function(chunk){ responseString.push(chunk); });

		res.on('end', function(){
			  var response_body = iconv.decode(Buffer.concat(responseString), 'utf8'); //gb2312 win1252
			  console.log(response_body);
		});
		res.on('error', function(e){
			console.log('problem with request: '+e.message);
		});

});

req.write(postData); req.end();

QQ截图20160525095731.png

返回的json乱码 请大神赐教

7 回复

编码用utf-8,解码用win1252,对于这种代码很无语

@bwpAj 就不能排版一下吗?

@FoghostCn 这个即使用了utf-8解码 也试过了 也没有啥用 所以司马当活码医 各种试

你的是wins平台?有没有试过设置下cmd的编码

gzip压缩 不解压缩?

@ncuzp 应该cmd编码无关 用另个方法解决了 var request = require(‘request’); 能返回正常的数据

@vince78 用 var request = require(‘request’); 这个第三方模块解决了 能返回正常的数据

回到顶部