Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request data type change to string on retry #295

Open
tomerparizer opened this issue Dec 26, 2024 · 1 comment
Open

Request data type change to string on retry #295

tomerparizer opened this issue Dec 26, 2024 · 1 comment

Comments

@tomerparizer
Copy link

Hey

I see a weird behavior happening inside the retry mechanism
Once inside the retry phase the request body change to string instead of JSON
As you can see in the simple example below

const axios = require('axios');
const axiosRetry = require('axios-retry').default;

axios.interceptors.request.use((config) => {
  console.log('in interceptor', config.data, typeof config.data);
  return config;
});

axiosRetry(axios, { retries: 3, retryCondition: () => true });

async function main() {
  try {
    const body = {
      name: 'example',
    };

    const { data } = await axios.post('http://localhost:3030/return500', body);
    console.log('got data', data);
  } catch (e) {
    console.log('got an issue', e.message);
  }
}

main();

The outcome is:

in interceptor { name: 'example' } object
in interceptor {"name":"example"} string
in interceptor {"name":"example"} string
in interceptor {"name":"example"} string
got an issue Request failed with status code 500

Can you please help?

Thanks

@tomerparizer
Copy link
Author

Hey
After checking a little in the axios side looks like it is how the axios response interceptor is getting the data

In the following code (that doesnt use axios-retry) you can see that the data is string instead of json

const axios = require('axios');

axios.interceptors.request.use((config) => {
  console.log('in req interceptor', config.data, typeof config.data);
  return config;
});

axios.interceptors.response.use(null, (error) => {
  console.log('in res interceptor', error.config.data, typeof error.config.data);
  throw error;
});

async function main() {
  try {
    const body = {
      name: 'example',
    };

    const { data } = await axios.post('http://localhost:3030/return500', body);
    console.log('got data', data);
  } catch (e) {
    console.log('got an issue', e.message);
  }
}

main();

The outcome is:

in req interceptor { name: 'example' } object
in res interceptor {"name":"example"} string
got an issue Request failed with status code 500

My guess is that the response interceptor is using the config after it was serialized to string and it is not being parsed again to json object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant