Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to pass an array within a query string?

Is there a standard method of going an array through an inquiry string?

All things considered, I have an inquiry string with various qualities, one of which would be array value. I need that question string value to be treated as an array I don't need the array to be detonated so it is undefined from the other inquiry string variables.

Likewise, as indicated by this post answer, the creator recommends that question string support for arrays isn't characterized. Is this correct?

Would it be acceptable to name multiple params the same name, and that way I would know that they belong to an array? Example:

?myarray=value1&myarray=value2&myarray=value3...


Or would this be a bad practice?
by

3 Answers

aashaykumar
A query string carries textual data so there is no option but to explode the array, encode it correctly and pass it in a representational format of your choice:

p1=value1&pN=valueN...
data=[value1,...,valueN]
data={p1:value1,...,pN:valueN}


and then decode it in your server side code.
pankajshivnani123
Although there isn't a standard on the URL part, there is one standard for JavaScript. If you pass objects containing arrays to URLSearchParams, and call toString() on it, it will transform it into a comma-separated list of items:

let data = {
str: 'abc',
arr: ['abc', 123]
}

new URLSearchParams(data).toString();
// ?str=abc&arr=abc,123 (with escaped comma characters)
RoliMishra
I feel it would be helpful for someone who is looking for passing the array in a query string to a servlet. I tested below query string and was able to get the array values using req.getgetParameterValues(); method. Below is the query string I passed through browser.

 http://localhost:8080/ServletsTutorials/*.html? 
myname=abc&initial=xyz&checkbox=a&checkbox=b

Login / Signup to Answer the Question.