Home > Back-end >  axios multipe param request
axios multipe param request

Time:01-11

I'm trying to make a query with axios and parameters When I perform this code:

axios.get('/myApi', { params: { id: [1,2,3] })

This puts all in the same url

http://localhost/api/myApi?id[]=1&id[]=2&id[]=3

Is there a possibility that I will get a different request each time knowing that my input table will be dynamic?

I would like this dynamically:

http://localhost/api/myApi?id[]=1
http://localhost/api/myApi?id[]=3
http://localhost/api/myApi?id[]=3
...

thanks in advance

CodePudding user response:

No but you can loop through that url params and request it using axios like :-

for (let i = 0; i < id.length; i  ) {
  axios.get('/myApi', { params: { id: [i] })
}

CodePudding user response:

No you have to do that yourself, by issuing multpile requests.

You can utelize Promise.all and map for that.

map is used to issue the individual requests for the passed ids, and to store the Promises returned by axios in an array. And Promise.all to wait for all of them to resolve.

function batchRequest(ids) {
   return Promise.all(ids.map( id => {
      return axios.get('/myApi', { params: { id: [id] }})
   }))
}

  •  Tags:  
  • Related