iron/params

Improper query string parsing

Closed this issue · 1 comments

For URL with query part like that ?foo=bar&foo=baz&q=1&z[]=2&z[]=3 parser returns following:

println!("{:?}", req.get_ref::<Params>());
Ok({"foo": "baz", "q": "1", "z": ["2", "3"]})

I suppose that value for foo key should be an array too and following result should be expected:

Ok({"foo": ["bar", "baz"], "q": "1", "z": ["2", "3"]})

Code example:

extern crate iron;
extern crate params;

use iron::prelude::*;
use iron::status;
use params::Params;

fn main() {
    fn qs_parse(req: &mut Request) -> IronResult<Response> {
        let map = req.get_ref::<Params>();
        Ok(Response::with((status::Ok, format!("{:?}", map))))
    }

    Iron::new(qs_parse).http("localhost:3000").unwrap();
}

Ultimately, there is no standard that guarantees that the server interprets the query string one way or the other. Admittedly, my inspiration for this crate come mostly from Ruby on Rails, which handles duplicate parameters in just this way (x[]=1&x[]=2 yields { "x": ["1", "2"] }; x=1&x=2 yields { "x": "2" }).

There a lot of opinion baked into this crate (e.g. boolean interpretation, similarly inspired by Rails). As such, there's a ton of room for bikeshedding. 😕

Are you in a position where you're unable to append the [] suffix to array-like parameters?