how to get query parameter in lua or nginx?
Asked Answered
B

3

11

I am trying to implement this- https://gist.github.com/MendelGusmao/2356310 Lua,nginx based URL shortener,The only change i want to implement is when some query string parameter comes with shortened URL i need to take that parameter and insert into the long URL.

e.g. http://google.com?test=2 will be like http://abc.in/abc while hitting on http://abc.in/abc?test=3 I get redirected to - http://google.com?test=3.

For that i need to take query string parameters from $request_URI, can any one help with some code?

Billyebilobate answered 1/10, 2014 at 3:20 Comment(1)
D
27

You should be able to use ngx.var.arg_name where name is the name of the query parameter you want to access. See Variables with Infinite Names section in this tutorial for details on query parameter handling; you may also check my blog post for Lua nginx/openresty examples.

As an alternative, you can use ngx.req.get_uri_args() to retrieve all query parameters as one table. See this section in the same tutorial for the brief comparison between these methods.

Drumlin answered 1/10, 2014 at 4:38 Comment(2)
How does the table returned by get_uri_args differ from the table referred to by ngx.var.args?Kwangchow
ngx.var allows retrieving individual arguments when you know their names (through the use of metafields, these values actually don't exist in the table); get_uri_args() allows retrieving all passed arguments.Drumlin
H
7

You can also use ngx.var.QUERY_STRING to access the query string and unescape and parse it.

Hirschfeld answered 29/4, 2016 at 0:52 Comment(0)
R
4

You can obtain the query parameter with just nginx by using $arg_test, test is the name of the query parameter in this example.

This is documented in http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_.

Refuel answered 30/5, 2018 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.