Graph API 通过 axios 调用在特殊字符编码方面有问题
Graph API call via axios has trouble with special character encoding
当我在 Graph Explorer 中搜索包含特殊字符的用户时:
https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,'jörg')
效果很好,我得到了正确的结果集,例如:
{
...
"displayName": "Jörg XYZ",
"givenName": "Jörg",
...
},
现在在我的 node.js
项目中,我尝试通过以下方式对 axios
做同样的事情:
axios.default.get('https://graph.microsoft.com/v1.0//users?$filter=startswith(displayName,\'jö\')')
但我得到一个 空 结果集。
所以我通过以下方式将 jö 更改为 joe:
axios.default.get('https://graph.microsoft.com/v1.0//users?$filter=startswith(displayName,\'joe\')')
而且我只得到名字中确实有 Joe 的用户。但我需要 Jö。那么这里发生了什么?
我已经尝试通过以下方式设置字符集:
const options = {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json;charset=ISO-8859-1'
}
};
没有成功。我还能尝试什么?
最后很简单。我唯一错过的是在触发查询之前对名称进行 encoding:
let nameEncoded = encodeURI(name);
axios.default.get(`https://graph.microsoft.com/v1.0//users?$filter=startswith(displayName,'${nameEncoded}')`)
当我在 Graph Explorer 中搜索包含特殊字符的用户时:
https://graph.microsoft.com/v1.0/users?$filter=startswith(displayName,'jörg')
效果很好,我得到了正确的结果集,例如:
{
...
"displayName": "Jörg XYZ",
"givenName": "Jörg",
...
},
现在在我的 node.js
项目中,我尝试通过以下方式对 axios
做同样的事情:
axios.default.get('https://graph.microsoft.com/v1.0//users?$filter=startswith(displayName,\'jö\')')
但我得到一个 空 结果集。
所以我通过以下方式将 jö 更改为 joe:
axios.default.get('https://graph.microsoft.com/v1.0//users?$filter=startswith(displayName,\'joe\')')
而且我只得到名字中确实有 Joe 的用户。但我需要 Jö。那么这里发生了什么?
我已经尝试通过以下方式设置字符集:
const options = {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json;charset=ISO-8859-1'
}
};
没有成功。我还能尝试什么?
最后很简单。我唯一错过的是在触发查询之前对名称进行 encoding:
let nameEncoded = encodeURI(name);
axios.default.get(`https://graph.microsoft.com/v1.0//users?$filter=startswith(displayName,'${nameEncoded}')`)