简体   繁体   English

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

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

I am unable to get the data in the response of my HTTP GET request to Dynamics CRM in a format that is readable. 我无法以可读格式获取HTTP GET请求对Dynamics CRM的响应中的数据。 It always returns as unicode characters (ie.body:'\ \\b\\\\\\\ \\m ۸\ +Ķ=\\ Z \A7/ \ ... ' 它始终以unicode字符返回(即正文:'\ \\ b \\ u0000 \\ u0000 \\ u0000 \\ u0000 \\ u0000 \\ u0004 \ \\ m ۸ \ +Ķ= \\ Z \\ u0004A7 / \\ u000b ...'

When I send this same exact GET request in Postman, the body of the response I receive is formatted in a readable way and returns all of the KnowledgeArticles that I need - so (as far as I know) the http request is fine (as long as authorization token is kept current). 当我在Postman中发送相同的GET请求时,我收到的响应的主体将以可读的方式进行格式化,并返回我需要的所有KnowledgeArticles-因此(据我所知),http请求很好(只要因为授权令牌保持最新状态)。

I am just totally stuck on how to parse this unicode data in my response body in to readable text that I can use in my code logic to return the right results to the user. 我只是完全停留在如何将响应正文中的unicode数据解析为可读的文本中,以便在代码逻辑中使用该文本以将正确的结果返回给用户。

Below is my code for parsing calling the get request and parsing response 下面是我用于解析调用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);
});

You got compressed response , remove 'accept-encoding': 'gzip, deflate' header 您得到了压缩的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"
    }
}

or add gzip: true to request options 或添加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
};

or manually decompress your response 或手动解压缩您的response

I believe what you're looking for is JSON.parse() . 我相信您正在寻找的是JSON.parse()

Here's a full example created by Jason Lattimer's CRMRESTBuilder . 这是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.

相关问题 Dynamics 2011 CRM表单我试图隐藏选项卡,或者它是onload中的部分 - Dynamics 2011 CRM form I am trying to hide a tab and or it's sections from the onload HTTP POST响应在字母之间返回&#39;\\ u0&#39;字符,如何在JavaScript中删除它们或解析它们? - An HTTP POST response is returning '\u0' characters between letters, how do I remove those or parse them in JavaScript? Node.js与HTTP响应主体的unicode问题 - Node.js unicode issue with HTTP response body 我想在Dynamics 365 crm中从一种形式重定向到另一种形式 - I want to redirect from one form to another in dynamics 365 crm FormType 未在 Dynamics CRM 2016 上返回正确的值 - FormType not returning proper value on Dynamics CRM 2016 无法显示来自unicode JSON响应的表情符号 - Unable to display emoji from unicode JSON response 将返回值从内部函数返回到父函数-Javascript,Dynamics crm - Returning the return value from inner function to parent function- Javascript, Dynamics crm 为什么我无法从此jsonp响应中获取位置? - Why am I unable to get the location from this jsonp response? Lambda不会通过对API网关的http响应返回正文 - Lambda isn't returning the body with an http response to API Gateway 从 http 响应正文中获取特定值 - Getting specific value from http response body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM