mbob
mbob

Reputation: 630

Messed data object in response with Axios

I'm making a request for getting an access_token to an auth0 API. The request is success, but the data object containing the access token contains weird characters. The thing is that I had that object for 3-4 hours, after that it wasn't retrieved anymore. Any clues on this?

enter image description here

And this is the code:

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

Upvotes: 2

Views: 76

Answers (1)

Bench Vue
Bench Vue

Reputation: 9380

In v1.2.1 fixed this error.

You need to add Accept-Encoding with 'application/json' in axios header.

The default of axios is gzip.

This code will be works

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

Upvotes: 1

Related Questions