node.js+;请求=>;显示垃圾响应的响应
我使用node.js+请求将HTTP请求发送到URL。我需要JSON响应,但我得到了垃圾字符 这是我的node.js代码node.js+;请求=>;显示垃圾响应的响应,node.js,http,request,Node.js,Http,Request,我使用node.js+请求将HTTP请求发送到URL。我需要JSON响应,但我得到了垃圾字符 这是我的node.js代码 var request = require('request'); var post_data = { UserName: "xxxxx", Password: "xxxxx" }; var post_options = { url: 'http://xxxxxxx.info/api/CoreUser/cognitoLogin', method: 'POST',
var request = require('request');
var post_data = { UserName: "xxxxx", Password: "xxxxx" };
var post_options = {
url: 'http://xxxxxxx.info/api/CoreUser/cognitoLogin',
method: 'POST',
form: post_data,
headers: {
'AppID': 'zNiphaJww8e4qYEwJ96g555HTAAbAXdj',
'OAuth': 'xxxxxxxxxxxxxxxxxx',
//'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/json;charset=UTF-8',
}
};
// Set up the request
request(post_options, function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
console.log("200");
console.log(response);
}
});
但我收到了垃圾角色的回应
我需要jSON格式的结果,这个请求有什么问题吗?请求调用中缺少
{jSON:true}
request(post_options, { json: true }, function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
console.log("200");
console.log(response);
}
});
我发现了这个问题,原因是API响应以gZip格式发送。这是我们必须在这里做的改变。只需启用解决问题的
gzip:true
var request = require('request');
var post_data = { UserName: "xxxxx", Password: "xxxxx" };
var post_options = {
url: 'http://xxxxxxx.info/api/CoreUser/cognitoLogin',
method: 'POST',
gzip: true,
form: post_data,
headers: {
'AppID': 'zNiphaJww8e4qYEwJ96g555HTAAbAXdj',
'OAuth': 'xxxxxxxxxxxxxxxxxx',
//'User-Agent': 'Super Agent/0.0.1',
'Content-Type': 'application/json;charset=UTF-8',
}
};
// Set up the request
request(post_options, function (error, response, body) {
console.log(response.statusCode);
if (!error && response.statusCode == 200) {
console.log("200");
console.log(response);
}
});