繁体   English   中英

我无法从Dynamics CRM解析HTTP响应正文,它始终以Unicode字符形式返回

[英]I am unable to parse an HTTP response body from Dynamics CRM, it keeps returning as unicode characters

我无法以可读格式获取HTTP GET请求对Dynamics CRM的响应中的数据。 它始终以unicode字符返回(即正文:'\ \\ b \\ u0000 \\ u0000 \\ u0000 \\ u0000 \\ u0000 \\ u0004 \ \\ m ۸ \ +Ķ= \\ Z \\ u0004A7 / \\ u000b ...'

当我在Postman中发送相同的GET请求时,我收到的响应的主体将以可读的方式进行格式化,并返回我需要的所有KnowledgeArticles-因此(据我所知),http请求很好(只要因为授权令牌保持最新状态)。

我只是完全停留在如何将响应正文中的unicode数据解析为可读的文本中,以便在代码逻辑中使用该文本以将正确的结果返回给用户。

下面是我用于解析调用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);
});

您得到了压缩的response ,删除了'accept-encoding': 'gzip, deflate'标头

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

我相信您正在寻找的是JSON.parse()

这是Jason Lattimer的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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM