soveran / cuba

Rum based microframework for web development.
http://cuba.is
MIT License
1.44k stars 249 forks source link

Post Request with multiple parameters with the same name #59

Closed moonglum closed 9 years ago

moonglum commented 9 years ago

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?

frodsan commented 9 years ago

Can you try name="players[]"?

soveran commented 9 years ago

Yes, it's indeed how Rack works. The workaround is to add [] as @frodsan described.

moonglum commented 9 years ago

Thank you so much, this fixed it!

(What is this dark magic :confused:)