Configure custom paramsSerializer

seanwash
0
seanwash
commented a year ago

How might one go about configuring a custom paramsSerializer for Axios? I'm working on an app that uses nested query string params and I need to use the qs lib with a different arrayFormat, but I'm not sure how to modify the global instance of Axios when using this module. What I'd like to do is something like:

  axios: {
    baseURL: process.env.API_URL,
    paramsSerializer: (params) => {
      return qs.stringify(params, { arrayFormat: 'brackets' })
    }
  }
0
seanwash
0
seanwash
commented a year ago

I was able to set this up by using requestInterceptor and modifying the passed config to include paramsSerializer. Feel free to close this unless you have any suggestions?

  axios: {
    baseURL: process.env.API_URL,
    requestInterceptor: (config, { store }) => {
      var qs = require('qs')
      config.paramsSerializer = (params) => {
        return qs.stringify(params, { arrayFormat: 'brackets' })
      }
      return config
    }
  }
0
pi0
19.8k
pi0
commented a year ago

Hi @seanwash. Sorry for late answer. I think using interceptors for customization would be the best option.

0
ibufu
0
ibufu
commented a year ago
axios.defaults.paramsSerializer = params => {
  return qs.stringify(params, {arrayFormat: 'repeat'});
};
0
Maden-maxi
0
Maden-maxi
commented 2 months ago

It's can be change via plugin.

// ~/plugins/axios.js 
import qs from 'qs';

export default function ({$axios, app, store}) {

  $axios.onRequest(config => {
    config.paramsSerializer = params => qs.stringify(params, { arrayFormat: 'brackets' });
    return config;
  });
}
0
leohdr
0
leohdr
commented 14 days ago
axios.defaults.paramsSerializer = params => {
  return qs.stringify(params, {arrayFormat: 'repeat'});
};

not working

0
Informations
Question โ€ข Unresolved
#c8 - Created a year ago