gpt4 book ai didi

javascript - 我无法解析 Dynamics CRM 中的 HTTP 响应正文,它始终以 unicode 字符形式返回

转载 作者:行者123 更新时间:2023-12-02 23:30:15 24 4
gpt4 key购买 nike

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

当我在 Postman 中发送同样的 GET 请求时,我收到的响应正文以可读的方式格式化并返回我需要的所有知识文章 - 所以(据我所知)http 请求没问题(只要授权 token 保持最新)。

我完全陷入了如何将响应正文中的 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);
});

最佳答案

您获得了压缩的响应,请删除'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
};

或手动解压缩您的响应

关于javascript - 我无法解析 Dynamics CRM 中的 HTTP 响应正文,它始终以 unicode 字符形式返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56547515/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com