How to access query parameter names with '-' in it

If query parameters in the request looks like this

/something.cgi?param_name=1&param-name=2

I can access ‘param_name’ as $arg_param_name. How do I access
‘param-name’
though? $arg_param-name will not work since as far as nginx is concerned
the
variable name ends at $arg_param. Is there a workaround for this case?
Has
anyone else run into a similar problem?

Posted at Nginx Forum:

The solution is to access it using lua as ngx.var[ “arg_param-name” ].

Posted at Nginx Forum:

On Wed, Nov 12, 2014 at 03:38:03AM -0500, sudarshan wrote:

Hi there,

If query parameters in the request looks like this

/something.cgi?param_name=1&param-name=2

I can access ‘param_name’ as $arg_param_name. How do I access ‘param-name’
though? $arg_param-name will not work since as far as nginx is concerned the
variable name ends at $arg_param. Is there a workaround for this case? Has
anyone else run into a similar problem?

If you want to avoid embedded languages, you could match-and-extract
from $args, either with a map or an if, to set your variable name to
the matching value in “&?param-name=([^&]*)”.

But it might be less work to change the input to restrict itself to
nginx-friendly characters.

f

Francis D. [email protected]

You can also accomplish this without matching in a map:

map $pipe $dashed_param {
default $arg_param-name;
}

(I tried a geo block first, but it expanded to a literal
‘$arg_param-name’
for some reason)

Hello!

On Thu, Nov 13, 2014 at 09:38:17AM -0500, Jonathan K. wrote:

You can also accomplish this without matching in a map:

map $pipe $dashed_param {
default $arg_param-name;
}

While this may currently work, I wouldn’t recommend relying on
this - as this is rather a bug than a desired behaviour. This
will stop working as long as support for multiple variables will
be added to map.

Instead, I would recommend to use regexp-based parsing of the
$args variable as Francis suggests. This can be done trivially
with map, like this:

map $args $param_with_dash {
    "~(^|&)param-name=(?<temp>[^&]+)"  $temp;
}

(I tried a geo block first, but it expanded to a literal ‘$arg_param-name’
for some reason)

The geo module only support static strings as values, so that’s
expected behaviour.

the matching value in “&?param-name=([^&]*)”.
[email protected]
nginx Info Page


nginx mailing list
[email protected]
nginx Info Page


Maxim D.
http://nginx.org/