-
Notifications
You must be signed in to change notification settings - Fork 43
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
Encoding query arrays with name[]
convention
#185
Comments
Potentially duplicate of #41 |
Indeed. So at least could we add a mention in the doc, or throw/log an error in this case? |
As you probably know, this is URL standard behavior to duplicate params with same key name: const p = new URLSearchParams()
p.append('q', 'foo')
p.append('q', 'bar')
console.log(p.toString()) // q=foo&q=bar But i agree that it is was another common way to encode array params mainly in PHP world to use URL standard encodes the key as well:
For ufo, i think we might think about serializing it as After this i think documenting array encoding in ofetch would be a nice idea when patch arrives, feel free to open an issue or directly draft PR in ofetch for this ❤️ |
name[]
convention
Actually, it already works with a |
Amazing! Only i think ufo needs encoding change still 😅 (unless laravel already handles decoding in param names) import { withQuery } from "ufo";
console.log(
withQuery("/", {
"q[]": ["foo", "bar"],
})
); outputs
|
update: |
This is very problematic in nuxt from the backend as there is no distinctions when passing an array with a single key, this results in very unexpected results when there is only 1 element or 2 elements, could we maybe add an option for this? useFetch('/api/test', params: { foo: 'bar', bar: ['baz'] }); // foo=bar&bar=baz
// From the backend
console.log(getQuery(event)); // { "foo": "bar", "bar": "baz" }
useFetch('/api/test', params: { foo: 'bar', bar: ['baz', 'foo'] }); // foo=bar&bar=baz&bar=foo
// From the backend
console.log(getQuery(event)); // { "foo": "bar", "bar": ["baz", "foo"] } |
Environment
Node v21.1.0
[email protected]
Reproduction
Describe the bug
When using withQuery with an array, the param is duplicated instead of having the array syntax.
ex:
Expected:
http://ufo.test?q[]=foo&q[]=bar
Actual:
http://ufo.test?q=foo&q=bar
Additional context
Thanks for you work!
Logs
No response
The text was updated successfully, but these errors were encountered: