Kalanj Djordje Djordje
Kalanj Djordje Djordje

Reputation: 1352

Angular2 UrlSearchParams how to create url query with multiple values for same param with brackets

How to properly use UrlSearchParams to create url a structure like the following:

example.org?tag[]=one&tag[]=two&tag[]=other

When I use Url Params as presented below:

let params = new URLSearchParams();
params.append('tag', 'one');
params.append('tag', 'two');
params.append('tag', 'other');

The Url looks like this:

example.org?tag=one&tag=two&tag=other

This approach causes some problems on web servers because they treat that query string the same way as ?tag=one. The server gets only the first value if there are no brackets present.

Upvotes: 5

Views: 4952

Answers (2)

Nenad Nikolic
Nenad Nikolic

Reputation: 118

I think you are looking for this:

let params = new URLSearchParams();
params.append('tag[]', 'one');
params.append('tag[]', 'two');
params.append('tag[]', 'other');

Upvotes: 9

Poul Kruijt
Poul Kruijt

Reputation: 71901

I guess you can do something like this, but I haven't tested it:

let params = new URLSearchParams();
params.append('tag[0]', 'one');
params.append('tag[1]', 'two');
params.append('tag[2]', 'other');

Upvotes: 2

Related Questions