Axios - How not to add [] to the multi parameters?

Viewed 615

Axios adds square brackets to the param if there are multiple params with an equal name. The problem is that I need not to include those brackets since I use Django Rest Framework and Django Filters.

How can I do that if I use this notation?

axios.get(list_url, {params: {somelist:[1,2,3]}})

I don't want url/?somelist[]=1&somelist[]=2&somelist[]=3 Instead I want url/?somelist=1&somelist=2&somelist=3

1 Answers

You can do like this with URLSearchParams (a native object that exists in browsers and in Node.js as well):

const axios = require('axios');
const { URLSearchParams } = require('url');

var params = new URLSearchParams();
params.append("q", 'test');
params.append("foo", 2);
params.append("foo", 11);
var request = {
  params: params
};
axios.get('http://google.com/', request).then(x => x.request).then(console.log);
Related