Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing querystring vars #15

Closed
golgote opened this issue Jan 27, 2011 · 9 comments
Closed

Accessing querystring vars #15

golgote opened this issue Jan 27, 2011 · 9 comments

Comments

@golgote
Copy link

golgote commented Jan 27, 2011

I actually have two questions related to querystring vars, those are just to start a discussion :)

First I was wondering why you prefix the variables with 'arg_' when they are accessed with ngx.var["arg_num"] for example. Is this the way nginx stores them internally ? I thought it could make more sense to just use ngx.var["num"], this way no need to prefix variable names with "arg_". I realize this is a BC break though, so maybe having ngx.vars["num"] or like with PHP, ngx._get["num"] and ngx._post["num"] to make a difference between variables from GET, POST (and COOKIE, ENV actually...).

Then, I was wondering if there was a way to deal with request vars with the same name, for example : ?num=1&num=2
IIRC, mod_lua from Apache 2.4 converts the variable to a table in this case. PHP on the other side insist on having brakets like this : ?num[]=1&num[]=2 which can be handy because it also works with num[x]=1&num[y]=2 to create an associative array.

Maybe all of this could be done on the Lua side if I parse ngx.var["request_uri"] or the body. I just thought it could be easier if it was done in the module directly.

@agentzh
Copy link
Member

agentzh commented Jan 27, 2011

On Thu, Jan 27, 2011 at 6:30 PM, GitHub noreply@github.com wrote:

golgote reported an issue:

I actually have two questions related to querystring vars, those are just to start a discussion :)

First I was wondering why you prefix the variables with 'arg_' when they are accessed with ngx.var["arg_num"] for example.

If you do read ngx_lua's documentation more carefully, the syntax ngx.var.FOO is how to access nginx variables $FOO from within Lua. So ngx.var.arg_FOO is accessing nginx's special variable $arg_FOO which is documented here:

http://wiki.nginx.org/NginxHttpCoreModule#.24arg_PARAMETER

Is this the way nginx stores them internally ?

Because nginx's builtin variables $arg_FOO already provide this functionality, we do not provide any special interface in ngx_lua.

I thought it could make more sense to just use ngx.var["num"], this way no need to prefix variable names with "arg_". I realize this is a BC break though, so maybe having ngx.vars["num"] or like with PHP, ngx._get["num"] and ngx._post["num"] to make a difference between variables from GET, POST (and COOKIE, ENV actually...).

Yeah, it makes sense to provide native interface to access those parameters just like PHP. We'll work on that.

Then, I was wondering if there was a way to deal with request vars with the same name, for example : ?num=1&num=2

Yeah, for now you need to parse the query string in ngx.var.query_string yourself then...sorry about that.

IIRC, mod_lua from Apache 2.4 converts the variable to a table in this case. PHP on the other side insist on having brakets like this : ?num[]=1&num[]=2 which can be handy because it also works with num[x]=1&num[y]=2 to create an associative array.

Indeed :)

Maybe all of this could be done on the Lua side if I parse ngx.var["request_uri"] or the body. I just thought it could be easier if it was done in the module directly.

Agreed :)

Thanks!
-agentzh

agentzh added a commit that referenced this issue Aug 12, 2011
…lves github issue #15. thanks Bertrand Mansion (golgote).
@agentzh
Copy link
Member

agentzh commented Aug 12, 2011

I've already implemented the ngx.req.get_query_args() method in git master. Could you please give it a try? Please take a look at the documentation for details. Thanks!

@agentzh
Copy link
Member

agentzh commented Aug 12, 2011

I've also implemented the ngx.req.get_post_args() method :)

@agentzh
Copy link
Member

agentzh commented Aug 12, 2011

consider it resolved :)

@subnetmarco
Copy link

I can't find ngx.req.get_query_args() in the current documentation at: http://wiki.nginx.org/HttpLuaModule

@agentzh
Copy link
Member

agentzh commented Jun 16, 2014

@thefosk It was later renamed to ngx.req.get_uri_args.

@subnetmarco
Copy link

Ok, thanks.

@dessite
Copy link

dessite commented Oct 6, 2016

Not sure if it is an issue or works as designed but I found that if you refer to query string by

ngx.say(ngx.var.arg_name)

it will get the query string case insensitive, so no difference if you had "name=nginx" or "Name=nginx" or "NaMe=nginx" in your request, but if you do

local query_string = ngx.req.get_uri_args()
ngx.say(query_string["name"])
ngx.say(query_string["Name"])

it is case sensitive so "name=nginx" is not the same as "Name=nginx" in your request

@agentzh
Copy link
Member

agentzh commented Oct 6, 2016

@dessite Yes, it is expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants