Angular2 UrlSearchParams how to create a url with multiple values ​​for the same parameter using brackets

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

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

When I use Url Params as below:

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

Url is as follows:

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

This approach causes some problems on web servers as they handle this query string in the same way as ?tag=one . The server receives only the first value if there are no brackets.

+5
source share
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'); 
+10
source

I think you can do something similar, but I have not tested it:

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

Source: https://habr.com/ru/post/1260149/


All Articles