How to Pass an Array Within a Query String

How to pass an array within a query string?

Here's what I figured out:

Submitting multi-value form fields, i.e. submitting arrays through GET/POST vars, can be done several different ways, as a standard is not necessarily spelled out.

Three possible ways to send multi-value fields or arrays would be:

  • ?cars[]=Saab&cars[]=Audi (Best way- PHP reads this into an array)
  • ?cars=Saab&cars=Audi (Bad way- PHP will only register last value)
  • ?cars=Saab,Audi (Haven't tried this)

Form Examples

On a form, multi-valued fields could take the form of a select box set to multiple:

<form> 
<select multiple="multiple" name="cars[]">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
</select>
</form>

(NOTE: In this case, it would be important to name the select control some_name[], so that the resulting request vars would be registered as an array by PHP)

... or as multiple hidden fields with the same name:

<input type="hidden" name="cars[]" value="Volvo">
<input type="hidden" name="cars[]" value="Saab">
<input type="hidden" name="cars[]" value="Mercedes">

NOTE: Using field[] for multiple values is really poorly documented. I don't see any mention of it in the section on multi-valued keys in Query string - Wikipedia, or in the W3C docs dealing with multi-select inputs.


UPDATE

As commenters have pointed out, this is very much framework-specific. Some examples:

Query string:

?list_a=1&list_a=2&list_a=3&list_b[]=1&list_b[]=2&list_b[]=3&list_c=1,2,3

Rails:

"list_a": "3", 
"list_b":[
"1",
"2",
"3"
],
"list_c": "1,2,3"

Angular:

 "list_a": [
"1",
"2",
"3"
],
"list_b[]": [
"1",
"2",
"3"
],
"list_c": "1,2,3"

(Angular discussion)

See comments for examples in node.js, Wordpress, ASP.net


Maintaining order:
One more thing to consider is that if you need to maintain the order of your items (i.e. array as an ordered list), you really only have one option, which is passing a delimited list of values, and explicitly converting it to an array yourself.

How to pass an array within a query string in HttpClient?

I think the best way is to add them to parameters as a string and have your back-end convert it back to an array or list.

let actorList = ['Elvis', 'Jane', 'Frances']
let params = new HttpParams();
params = params.append('actors', actorList.join(', '));
this.http.get(url, { params: params });

How to pass a number array within a query string in HttpClient?

I think the way to do it is convert the array elements to string and then in the backend convert back this to array.

let page_id = [1,2,4,7];
let params = new HttpParams();
params = params.append('pages', page_id.join(','));
this.http.get(url, { params: params });

Passing array into query string

There is a limit on URL length on multiple levels (browsers, proxy servers, etc). You can change maxQueryString (*1) but I would not recommend it if you expect real users to use your system.

It looks like downloads.aspx is your page. Put all those ids in temporary storage - (cache or database) and pass the key to this new entity in the request

*1: https://blog.elmah.io/fix-max-url-and-query-string-length-with-web-config-and-iis/

How to pass an array of objects through a query string and read them in asp.net Controller

You have to add from [FromQuery]

public IActionResult GetInvoiceNum(            
[FromQuery] string xDateTime,
[FromQuery] string BuyerName,
[FromQuery] double TotalBillAmount,
[FromQuery] InvItem[] items
)

and convert fields to properties by adding getters/setters

public class InvItem
{
public string ItemCode { get; set; }
public double Quantity { get; set; }
public double SaleValue { get; set; }
}

In react, how to pass an array of object as a query string to the endpoint url?

You can use querystring's stringify function instead of a custom function that exactly does what you are looking for.

querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
// Returns 'foo=bar&baz=qux&baz=quux&corge='

https://nodejs.org/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options

In your react app, import querystring(you don't need to add a dependency as it's a Node.js's built-in module)

import querystring from 'querystring'

const url = `http://myapp.com?${querystring.stringify(queryParamsObj)}`

How to pass an array in query parameter reactjs

Convert the array into string and pass the value in query param.

multiSelectHandler = (option) => {
const details = option.selectedItems;
const stringData = details.map(({value}) => `${value}`).join(',');
console.log(stringData);
};

Array: Details: Output in console

0: Object { value: "Iphone", label: "Iphone" }
​1: Object { value: "Samsung", label: "Samsung"}

After converting into string:Output in console, Iphone,Samsung

Now pass this stringData in queryparam



Related Topics



Leave a reply



Submit