Axios响应时数据对象出错

6tqwzwtp  于 2022-11-29  发布在  iOS
关注(0)|答案(1)|浏览(178)

我正在向auth 0 API请求获取access_token。请求成功,但包含access_token的数据对象包含奇怪的字符。问题是我拥有该对象3-4个小时,之后就再也没有检索到它。对此有什么线索吗?

这就是代码:

(async () => {
  const client = axios.create({
    baseURL: 'https://my_url_to.auth0.com/oauth/token',
    headers: {
      'Content-Type': 'application/json'
    }
  });

  Log.debug(body);

  try {
    const resp = await client.post('/', body);

    console.log(JSON.stringify(resp.data));
  } catch (e) {
    Log.error(e);
  }
})();
uhry853o

uhry853o1#

您需要在axios标头中添加带有“application/json”的Accept-Encoding
axios的默认设置是gzip。
此代码将起作用

(async () => {
  const client = axios.create({
    baseURL: 'https://my_url_to.auth0.com/oauth/token',
    headers: {
      'Content-Type': 'application/json',
      'Accept-Encoding': 'application/json'
    }
  });

  Log.debug(body);

  try {
    const resp = await client.post('/', body);

    console.log(JSON.stringify(resp.data));
  } catch (e) {
    Log.error(e);
  }
})();

相关问题