Ad

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. It always returns as unicode characters (ie.body:'\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0004\u0000�\m�۸\u0011�+Ķ=\��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).

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.

Below is my code for parsing calling the get request and parsing response


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);
});
Ad

Answer

You got compressed response, remove '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"
    }
}

or add gzip: true to request options

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

Ad
source: stackoverflow.com
Ad