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.
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.
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¶m2=value2 would be our final url.
I think you are looking for this:
let params = new URLSearchParams();
params.append('tag[]', 'one');
params.append('tag[]', 'two');
params.append('tag[]', 'other');
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With