-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
convertFormUrl behaves unexpectedly with arrays #26
Comments
Hey @sushain97! Just realized that you are talking about the formUrl method! Disregard my answer below, sorry 😉
Hmm I don't get this with wretch. I get :
Why? Wretch uses the standard class URLSearchParams to encode query strings. const params = new URLSearchParams();
params.append('foo', 'bar');
params.append('foo', 'baz');
params.append('abc', 'def');
console.log(params.toString());
// Prints foo=bar&foo=baz&abc=def If you want to use any custom encoding function it's pretty easy anyway 😉 : https://runkit.com/embed/74fh6tbh1mo2 var wretch = require("wretch")
var { URLSearchParams } = require('url')
const urlParams = new URLSearchParams()
urlParams.append('a', 1)
urlParams.append('a', 2)
console.log(
'Standard URLSearchParams: ' +
urlParams.toString()
)
// > "Standard URLSearchParams: a=1&a=2"
const obj = { a: [1, 2] }
wretch().polyfills({ URLSearchParams })
console.log(
'Wretch with the standard URLSearchParams: ' +
wretch('/').query(obj)._url
)
// > "Wretch with the standard URLSearchParams: /?a=1&a=2"
console.log(
'Wretch using your own custom string: ' +
wretch('/').query('a=[1,2]')._url
)
// > "Wretch using your own custom string: /?a=[1,2]" |
No worries; I was light on the prose in my original question. |
Okay, got a proper answer now! I had a look at But the good thing is that if you want to have the same output as wretch().formUrl(qs.stringify({ a: 1, a: 2 }) On a side note, wretch does not encode form urls properly indeed. It should do the same as for query strings, using URLSearchParams which is the standard. For instance this <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="/" method="GET">
<input type="text" name="a"/>
<input type="text" name="a"/>
<input type="submit" value='submit'/>
</form>
</body>
</html> I consider that the browsers behaviour is the standard one, so I am going to align wretch output with that! |
+💯
Sounds good! Unfortunately, that doesn't align with what I want, but you're right -- standard is best. Thanks for the workaround and your work on this great library. |
https://runkit.com/embed/gh39bs0gaifx:
==>
I expect something like the second.
The text was updated successfully, but these errors were encountered: