Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angular

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.

like image 368
Kalanj Djordje Djordje Avatar asked Nov 22 '16 15:11

Kalanj Djordje Djordje


People also ask

Which is the delimited to pass multiple parameters in query type of HTTP GET method?

Parameters are passed to the web application as part of an HTTP request in the form of ”name=value” strings. When multiple parameters are required they are delimited by the '&' character.

How do I add parameters to a URL in Python?

To send parameters in URL, write all parameter key:value pairs to a dictionary and send them as params argument to any of the GET, POST, PUT, HEAD, DELETE or OPTIONS request. then https://somewebsite.com/?param1=value1&param2=value2 would be our final url.


2 Answers

I think you are looking for this:

let params = new URLSearchParams();
params.append('tag[]', 'one');
params.append('tag[]', 'two');
params.append('tag[]', 'other');
like image 143
Nenad Nikolic Avatar answered Oct 30 '22 23:10

Nenad Nikolic


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');
like image 23
Poul Kruijt Avatar answered Oct 31 '22 00:10

Poul Kruijt