Javascript 我无法解析Dynamics CRM中的HTTP响应正文,它一直以unicode字符返回
我无法以可读的格式获取对Dynamics CRM的HTTP get请求响应中的数据。它总是以unicode字符返回,例如:body:'\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0004\u0000�\M�۸\u0011�+Ķ=\��Z���\u0004A7/�\u000b…' 当我在Postman中发送这个完全相同的GET请求时,我收到的响应的主体将以可读的方式格式化,并返回我需要的所有知识文章——据我所知,只要授权令牌保持最新,http请求就可以了 我完全被困在如何将响应体中的unicode数据解析为可读文本中,我可以在代码逻辑中使用这些文本将正确的结果返回给用户 下面是我用来解析调用get请求和解析响应的代码Javascript 我无法解析Dynamics CRM中的HTTP响应正文,它一直以unicode字符返回,javascript,node.js,unicode,dynamics-crm,httpresponse,Javascript,Node.js,Unicode,Dynamics Crm,Httpresponse,我无法以可读的格式获取对Dynamics CRM的HTTP get请求响应中的数据。它总是以unicode字符返回,例如:body:'\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0004\u0000�\M�۸\u0011�+Ķ=\��Z���\u0004A7/�\u000b…' 当我在Postman中发送这个完全相同的GET请求时,我收到的响应的主体将以可读的方式格式化,并返回我需要的所有知识文章——据我所知,只要授权令牌保持最新,http请求就
const restify = require('restify');
const errors = require('restify-errors');
const port = process.env.PORT || 3000;
const request = require("request");
const stringify = require('stringify');
const server = restify.createServer({
name: 'restify headstart'
});
server.listen(port, () => {
console.log(`API is running on port ${port}`);
});
ar options = { method: 'GET',
url: 'https://########.crm.dynamics.com/api/data/v9.1/knowledgearticles',
qs: { '$select': 'content,title' },
headers:
{ 'cache-control': 'no-cache',
Connection: 'keep-alive',
'accept-encoding': 'gzip, deflate',
cookie: '###################',
Host: '#####.crm.dynamics.com',
'Postman-Token': '#######',
'Cache-Control': 'no-cache',
'User-Agent': 'PostmanRuntime/7.13.0',
Authorization: 'Bearer ################# buncha crap #####',
Accept: 'application/json'
}
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
// below are all of my attempts at parsing 'response'
* let aaa = response;
* let aaa = response.toJSON();
* let aaa = JSON.stringify(response);
* let aaa = response.toString();
* let aaa = response.toJSON(body);
* let aaa = response.setEncoding('binary');
* let aaa = aaaa.toJSON();
// none of the above result in my response logging into readable text
console.log(aaa);
});
我相信您正在寻找的是JSON.parse 下面是由创建的完整示例 您得到了压缩响应,请删除“接受编码”:“gzip,deflate”标题 或者将gzip:true添加到请求选项
const options = {
method: "GET",
url: "https://contactcenter.crm.dynamics.com/api/data/v9.1/knowledgearticles",
qs: {"$select": "content,title"},
headers: {
"cache-control": "no-cache",
"connection": "keep-alive",
"accept-encoding": "gzip, deflate",
"cookie": "...",
"host": "contactcenter.crm.dynamics.com",
"postman-token": "...",
"User-Agent": "PostmanRuntime/7.13.0",
"authorization": "Bearer ...",
"accept": "application/json"
},
gzip: true
};
或者手动解压缩您的响应如果您得到了压缩响应,请删除“接受编码”:“gzip,deflate”标题,或者解压缩您的响应,或者将gzip:true添加到请求选项冷却!这很有效,非常感谢@ponury kostek-你介意把这条评论作为回答吗?@jsbueno按要求发布:Works。非常感谢。我道歉。我对这个很陌生。我现在就接受。
const options = {
method: "GET",
url: "https://contactcenter.crm.dynamics.com/api/data/v9.1/knowledgearticles",
qs: {"$select": "content,title"},
headers: {
"cache-control": "no-cache",
"connection": "keep-alive",
"cookie": "...",
"host": "contactcenter.crm.dynamics.com",
"postman-token": "...",
"User-Agent": "PostmanRuntime/7.13.0",
"authorization": "Bearer ...",
"accept": "application/json"
}
}
const options = {
method: "GET",
url: "https://contactcenter.crm.dynamics.com/api/data/v9.1/knowledgearticles",
qs: {"$select": "content,title"},
headers: {
"cache-control": "no-cache",
"connection": "keep-alive",
"accept-encoding": "gzip, deflate",
"cookie": "...",
"host": "contactcenter.crm.dynamics.com",
"postman-token": "...",
"User-Agent": "PostmanRuntime/7.13.0",
"authorization": "Bearer ...",
"accept": "application/json"
},
gzip: true
};