Post Request with multiple parameters with the same name
moonglum opened this issue · comments
Given I have a form on my website where I use a <select name="players" multiple>
, my browser will send a POST request with the application/x-www-form-urlencoded
request body "players=2&players=3". As you can see, it contains the key players
twice.
If I now get the value of req.POST
I get the value {"players"=>"3"}
. The second occurrence of the key overwrote the first one. The only place where I can see all selections is in req.env["rack.request.form_vars"]
. Here I will get the string "players=2&players=3"
.
So if I want to access the entire selection of the user, I will need to parse the form_vars
by hand.
Is there any other solution? This also seems to be at the Rack level, right? So this is probably not even in the source code of Cuba. But maybe someone has a solution for it?
Can you try name="players[]"
?
Yes, it's indeed how Rack works. The workaround is to add []
as @frodsan described.
Thank you so much, this fixed it!
(What is this dark magic 😕)