Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URLSearchParams with repeated parameters #851

Open
tomasdev opened this issue Dec 19, 2024 · 6 comments
Open

URLSearchParams with repeated parameters #851

tomasdev opened this issue Dec 19, 2024 · 6 comments

Comments

@tomasdev
Copy link

What is the issue with the URL Standard?

URLs such as foo.com/?q=1&q=2 are valid and distinct than foo.com/?q=1 and foo.com/?q=2.

Most server frameworks allow q to be a list containing all values ([1, 2] in the case above)

However, there's no way to construct such parameters using existing URLSearchParams API.

Is this intentionally not offered to dissuade use, or was there an RFC that said q=1&q=2 is a wrongly formatted URL?

@annevk
Copy link
Member

annevk commented Dec 19, 2024

Can you explain what you main with code?

const x = new URLSearchParams("?q=1&q=2");
console.log(x.getAll("q")); // ["1", "2"]
x.append("y", "3");
x.append("y", "4");
console.log(x.getAll("y")); // ["3", "4"]
w(x.toString()); // "q=1&q=2&y=3&y=4"

@nektro
Copy link

nektro commented Dec 19, 2024

my reading is that there's no way to create that using URLSearchParams.prototype.set (or a non-existant .setAll). eg:

let params = new URLSearchParams() 
params.set("q",[1,2]) 
params.toString() // "q=1%2C2"
params.get("q") // "1,2" 

@nektro
Copy link

nektro commented Dec 19, 2024

URLSearchParams.prototype.append looks like the way to go

let params = new URLSearchParams()
params.append("q", 1)
params.append("q", 2)
params.toString() // "q=1&q=2"
params.getAll("q") // [ "1", "2" ]

@annevk
Copy link
Member

annevk commented Dec 19, 2024

Maybe we should add some examples as to how you can use it as a map and how you can use it as a multi-map. The API has been designed such that it can be used for both. If you care about map, you'd primarily use get/set; if you care about multi-map, you'd use getAll/append. delete/has are useful for both.

Still curious what OP meant though.

@tomasdev
Copy link
Author

My bad. I did not see the getAll option.

@annevk
Copy link
Member

annevk commented Jan 2, 2025

Thanks for clarifying. Let's keep this open to track adding examples.

@annevk annevk reopened this Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants