normalizing query parameter names?

32 views
Skip to first unread message

mla

unread,
Feb 6, 2014, 7:27:39 PM2/6/14
to psgi-...@googlegroups.com
I'm upgrading a legacy app that is inconsistent with its query parameter names. e.g., sometimes "user-id" is used, whereas other times it's "user_id".
I'd like to normalize the parameter keys to underscores, so I don't have to worry about the inconsistencies from the front end, but I'm not sure how best to do that.

I was thinking I could override Hash::MultiValue but it doesn't look like there's a clean way to do that, since Plack::Request uses Hash::MultiValue directly.

Any thoughts?



Ævar Arnfjörð Bjarmason

unread,
Feb 6, 2014, 7:35:08 PM2/6/14
to psgi-...@googlegroups.com

If it's a legacy app then presumably it doesn't use the Plack::Request API. So instead of using that just use a thin wrapper that checks for both $str and $str =~ try/-_/_-/ ?

--
 
---
You received this message because you are subscribed to the Google Groups "psgi-plack" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psgi-plack+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jakob Voß

unread,
Feb 7, 2014, 2:10:01 AM2/7/14
to psgi-...@googlegroups.com
mla wrote:

> I'm upgrading a legacy app that is inconsistent with its query parameter
> names. e.g., sometimes "user-id" is used, whereas other times it's
> "user_id". I'd like to normalize the parameter keys to underscores, so
> I don't have to worry about the inconsistencies from the front end,
> but I'm not sure how best to do that.

I use a custom middleware for a similar purpose and thought about a
general Middleware for query string rewriting. Here is a boilerplate:

enable sub {
my ($app) = @_;
sub {
my ($env) = @_;

# modify QUERY_STRING (possibly by using a Plack::Request)

$env->{QUERY_STRING} = ...

# Unfortunately the query is not only stored QUERY_STRING but
# possibly in plack.request.merged and plack.request.query too.
# modifications to QUERY_STRING won't update the latter:

delete $env->{'plack.request.merged'};
delete $env->{'plack.request.query'};

$app->($env);
}
};

Jakob

--
Jakob Voß <jakob...@gbv.de>
Verbundzentrale des GBV (VZG) / Common Library Network
Platz der Goettinger Sieben 1, 37073 Göttingen, Germany
+49 (0)551 39-10242, http://www.gbv.de/

mla

unread,
Feb 7, 2014, 4:30:56 AM2/7/14
to psgi-...@googlegroups.com, vo...@gbv.de


On Thursday, February 6, 2014 11:10:01 PM UTC-8, Jakob Voß wrote:
mla wrote:

> I'm upgrading a legacy app that is inconsistent with its query parameter
> names. e.g., sometimes "user-id" is used, whereas other times it's
> "user_id". I'd like to normalize the parameter keys to underscores, so
 > I don't have to worry about the inconsistencies from the front end,
 > but I'm not sure how best to do that.

I use a custom middleware for a similar purpose and thought about a
general Middleware for query string rewriting. Here is a boilerplate:

Thanks. Yeah, I was hoping to avoid double parsing everything, but that might be the simplest. Query and body parameters would end up having different rules, which isn't ideal (I mainly care about the query parameters but it would be nice to be consistent).

@Ævar: I'm in the process of converting it to a Plack-based app. I wanted to avoid having to check multiple param formats.
 

Jakob Voß

unread,
Feb 7, 2014, 6:33:14 AM2/7/14
to psgi-...@googlegroups.com

> > I use a custom middleware for a similar purpose and thought about a
> > general Middleware for query string rewriting. Here is a boilerplate:
>
> Thanks. Yeah, I was hoping to avoid double parsing everything, but that
> might be the simplest. Query and body parameters would end up having
> different rules, which isn't ideal (I mainly care about the query
> parameters but it would be nice to be consistent).

This was on may TODO-List anyway, so I created
Plack::Middleware::Rewrite::Query, just released at CPAN.

To replace all minus with underscore in query parameter names:

builder {
enable 'Rewrite::Query', map => sub { $_[0] =~ s/-/_/g; @_ },
$app;
}

Cheers

mla

unread,
Feb 9, 2014, 12:27:39 AM2/9/14
to psgi-...@googlegroups.com, vo...@gbv.de


On Friday, February 7, 2014 3:33:14 AM UTC-8, Jakob Voß wrote:

> > I use a custom middleware for a similar purpose and thought about a
> >  general Middleware for query string rewriting. Here is a boilerplate:
 >
> Thanks. Yeah, I was hoping to avoid double parsing everything, but that
> might be the simplest. Query and body parameters would end up having
> different rules, which isn't ideal (I mainly care about the query
> parameters but it would be nice to be consistent).

This was on may TODO-List anyway, so I created
Plack::Middleware::Rewrite::Query, just released at CPAN.

To replace all minus with underscore in query parameter names:

builder {
     enable 'Rewrite::Query', map => sub { $_[0] =~ s/-/_/g; @_ },
     $app;
}

Awesome. Thanks, Jakob. 
Reply all
Reply to author
Forward
0 new messages