I am looping over an array of strings and want to pass these in chunks of 30 into the axios GET request as query params but I dont know how to do it correctly.
const ids = ["1","2", "3", "4", "5"] //up to 10K entries
What I need is 30 ids as a query parameter with the same key on each request like this
axios.get("/endpoint?id=1&id=2&id=3&id=4") ecetera. My approach doesnt work and I would welcome some tips on how to approach this correctly.
what I have
const assets = await getRepository(Asset).find({ collection: collection })
const token_ids = assets.map(a => {
return a.token_id
})
const asset_count = assets.length;
let config: AxiosRequestConfig = {
headers: this.header,
params: {
limit: 50,
offset: 0,
side: 1,
sale_kind: 0,
order_by: "eth_price",
order_direction: "asc",
asset_contract_address: assets[0].asset_contract.address
}
}
while (true) {
const ids = token_ids.splice(0,30); //get 30 ids for pagination (max)
//apply ids to params
for( let i=0; i< ids.length; i++){
config.params["id"] = ids[i]; //this wont work cause duplicate keys arent allows
}
const response = await axios.get(this.API_BASE_URL + "/orders", config)
//do something
}
config.params["id[]"] = ids[i], but considering you are talking about 10K entries, you gonna to exceed the limit of URL length, please consider to use post or split the query into chunks"id"is overridden, same was my suggestion, so you will need URLSearchParam instead, orconfig.params[`id[${i}]`] = ids[i]URLSearchParam#appenddoesn't override the key, it will append more value with same key