Axios-module: Configure custom paramsSerializer

Created on 17 Aug 2017  路  6Comments  路  Source: nuxt-community/axios-module

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' })
    }
  }

This question is available on Nuxt.js community (#c8)

Most helpful comment

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

All 6 comments

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'}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simllll picture simllll  路  6Comments

chrislentz picture chrislentz  路  3Comments

WtekSupport picture WtekSupport  路  6Comments

daolou picture daolou  路  3Comments

altafsayani picture altafsayani  路  3Comments