我无法解析来自 Dynamics CRM 的 HTTP 响应正文,它一直以 unicode 字符形式返回
I am unable to parse an HTTP response body from Dynamics CRM, it keeps returning as unicode characters
我无法以可读的格式获取我对 Dynamics CRM 的 HTTP GET 请求的响应中的数据。它总是 returns 作为 unicode 字符 (ie.body:'\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0004\u0000�\m�8\u0011�+Ķ=\��Z���\u0004A7/�\ u000b ... '
当我在 Postman 中发送完全相同的 GET 请求时,我收到的响应正文以可读的方式格式化并且 return 是我需要的所有 KnowledgeArticles - 所以(据我所知知道)http 请求没问题(只要授权令牌保持最新)。
我完全不知道如何将我的响应正文中的这个 unicode 数据解析为可读文本,我可以在我的代码逻辑中使用这些文本 return 为用户提供正确的结果。
下面是我解析调用get请求和解析响应的代码
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()
。
这是由 Jason Lattimer's CRMRESTBuilder 创建的完整示例。
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/knowledgearticles?$select=content,title", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var content = results.value[i]["content"];
var title = results.value[i]["title"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
您已压缩 response
,删除 'accept-encoding': 'gzip, deflate'
header
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"
}
}
或添加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
};
或手动解压缩您的 response
我无法以可读的格式获取我对 Dynamics CRM 的 HTTP GET 请求的响应中的数据。它总是 returns 作为 unicode 字符 (ie.body:'\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0004\u0000�\m�8\u0011�+Ķ=\��Z���\u0004A7/�\ u000b ... '
当我在 Postman 中发送完全相同的 GET 请求时,我收到的响应正文以可读的方式格式化并且 return 是我需要的所有 KnowledgeArticles - 所以(据我所知知道)http 请求没问题(只要授权令牌保持最新)。
我完全不知道如何将我的响应正文中的这个 unicode 数据解析为可读文本,我可以在我的代码逻辑中使用这些文本 return 为用户提供正确的结果。
下面是我解析调用get请求和解析响应的代码
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()
。
这是由 Jason Lattimer's CRMRESTBuilder 创建的完整示例。
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/knowledgearticles?$select=content,title", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
var content = results.value[i]["content"];
var title = results.value[i]["title"];
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
您已压缩 response
,删除 'accept-encoding': 'gzip, deflate'
header
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"
}
}
或添加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
};
或手动解压缩您的 response