How to Correctly Use Axios Params With Arrays

How to correctly use axios params with arrays

You can use paramsSerializer and serialize parameters with

axios.get('/myController/myAction', {
params: {
storeIds: [1,2,3]
paramsSerializer: params => {
return qs.stringify(params)

Multiple fields with same key in query params (axios request)?

From the axios documentation on the request config

// `params` are the URL parameters to be sent with the request
// Must be a plain object or a URLSearchParams object
params: {
ID: 12345

To use this in a request, you would do

var request = {
params: {
foo: [5, 2, 11]
axios.get('', request);

The only issue with using a plain object approach is that array parameters are added as[]=5&foo[]=2&foo[]=11

To get request without the [] like you want, you can use the URLSearchParams

var params = new URLSearchParams();
params.append("foo", 5);
params.append("foo", 2);
params.append("foo", 11);
var request = {
params: params
axios.get('', request);

This will result in a request as

pass array as parameter in an axios request

You can POST it as json data

let data=[1,2,3,4,5];
let json=JSON.stringify(data);
let post_data={json_data:json}'/url',post_data)
  • use JSON.stringify to convert it to json string
  • Use POST method to send data to server
  • Use json_decode to convert json back to array on server side

On laravel side you can do like below

$jsonArray = json_decode($response,true);

Parsing Axios URL Param to String Array in ExpressJS

You can turn the array to string with to toString:

await this.client.get("/endpoint", {
params: { query: ["max", "kevin"].toString() },

This will change the URL to:


Then in the express-validator you can validate the array items with wildcard:

req.check('query.*').isLength({ max: 30 })

Pass array as query params using Axios and Express router

You are defining your route with a route parameter, while it looks like you want the params to be regular query parameters.

That is, instead of reading from req.params, you want to read from req.query.

So instead of having this route

app.get('/get-pathways-with-partner/:partnerList', (req, res) => {
let { partnerList } = req.params;

You should have the following:

app.get('/get-pathways-with-partner/', (req, res) => {
let { partnerList } = req.query;

See the stackoverflow post at Node.js: Difference between req.query[] and req.params for more information.

Related Topics

Leave a reply