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' })
}
}
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
}
}
Hi @seanwash. Sorry for late answer. I think using interceptors for customization would be the best option.
axios.defaults.paramsSerializer = params => {
return qs.stringify(params, {arrayFormat: 'repeat'});
};
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;
});
}
axios.defaults.paramsSerializer = params => { return qs.stringify(params, {arrayFormat: 'repeat'}); };
not working
You should use {arrayFormat: 'brackets'} instead of {arrayFormat: 'repeat'}
Most helpful comment
It's can be change via plugin.