Skip to content

Commit 4418215

Browse files
committed
documented the ngx.req.get_query_args method. this new interface resolves github issue #15. thanks Bertrand Mansion (golgote).
1 parent 5f0744c commit 4418215

File tree

2 files changed

+126
-4
lines changed

2 files changed

+126
-4
lines changed

README.markdown

+57-4
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,61 @@ For reading request headers, use the `ngx.req.get_headers()` function instead.
945945
Reading values from ngx.header.HEADER is not implemented yet,
946946
and usually you shouldn't need it.
947947

948+
ngx.req.get_query_args()
949+
------------------------
950+
* **Context:** `rewrite_by_lua*`, `access_by_lua*`, `content_by_lua*`
951+
952+
Returns a Lua table holds all of the current request's request URL query arguments.
953+
954+
Here's an example,
955+
956+
location = /test {
957+
content_by_lua '
958+
local args = ngx.req.get_query_args()
959+
for key, val in pairs(args) do
960+
if type(val) == "table" then
961+
ngx.say(key, ": ", table.concat(val, ", "))
962+
else
963+
ngx.say(key, ": ", val)
964+
end
965+
end
966+
';
967+
}
968+
969+
Then `GET /test?foo=bar&bar=baz&bar=blah` will yield the response body
970+
971+
foo: bar
972+
bar: baz, blah
973+
974+
Multiple occurrences of an argument key will result in a table value holding all of the values for that key in order.
975+
976+
Keys and values will be automatically unescaped according to URI escaping rules. For example, in the above settings, `GET /test?a%20b=1%61+2` will yield the output
977+
978+
a b: 1a 2
979+
980+
Arguments without the `=<value>` parts are treated as boolean arguments. For example, `GET /test?foo&bar` will yield the outputs
981+
982+
foo: true
983+
bar: true
984+
985+
That is, they will take Lua boolean values `true`. However, they're different from arguments taking empty string values. For example, `GET /test?foo=&bar=` will give something like
986+
987+
foo:
988+
bar:
989+
990+
Empty key arguments are discarded, for instance, `GET /test?=hello&=world will yeild empty outputs.
991+
992+
Updating query arguments via the nginx variable `$args` (or `ngx.var.args` in Lua) at runtime are also supported:
993+
994+
ngx.var.args = "a=3&b=42"
995+
local args = ngx.req.get_query_args()
996+
997+
Here the `args` table will always look like
998+
999+
{a = 3, b = 42}
1000+
1001+
regardless of the actual request query string.
1002+
9481003
ngx.req.get_headers()
9491004
---------------------
9501005
* **Context:** `rewrite_by_lua*`, `access_by_lua*`, `content_by_lua*`
@@ -1105,16 +1160,14 @@ ngx.print(a, b, ...)
11051160

11061161
Emit args concatenated to the HTTP client (as response body).
11071162

1108-
Nil arguments are not allowed.
1163+
Lua nil value will result in outputing "nil", and Lua boolean values will emit "true" or "false".
11091164

11101165
ngx.say(a, b, ...)
11111166
------------------
11121167
* **Context:** `rewrite_by_lua*`, `access_by_lua*`, `content_by_lua*`
11131168

11141169
Just as `ngx.print` but also emit a trailing newline.
11151170

1116-
Nil arguments are not allowed.
1117-
11181171
ngx.log(log_level, ...)
11191172
-----------------------
11201173
* **Context:** `set_by_lua*`, `rewrite_by_lua*`, `access_by_lua*`, `content_by_lua*`
@@ -1339,7 +1392,7 @@ Compatibility
13391392

13401393
The following versions of Nginx should work with this module:
13411394

1342-
* 1.0.x (last tested: 1.0.4)
1395+
* 1.0.x (last tested: 1.0.5)
13431396
* 0.9.x (last tested: 0.9.4)
13441397
* 0.8.x >= 0.8.54 (last tested: 0.8.54)
13451398

t/030-query-args.t

+69
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,72 @@ GET /lua?foo===
257257
foo = ==
258258
done
259259

260+
261+
262+
=== TEST 10: empty key, but non-emtpy values
263+
--- config
264+
location /lua {
265+
content_by_lua '
266+
local args = ngx.req.get_query_args()
267+
local keys = {}
268+
for key, val in pairs(args) do
269+
table.insert(keys, key)
270+
end
271+
272+
table.sort(keys)
273+
for i, key in ipairs(keys) do
274+
ngx.say(key, " = ", args[key])
275+
end
276+
277+
ngx.say("done")
278+
';
279+
}
280+
--- request
281+
GET /lua?=hello&=world
282+
--- response_body
283+
done
284+
285+
286+
287+
=== TEST 11: updating args with $args
288+
--- config
289+
location /lua {
290+
content_by_lua '
291+
local args = ngx.req.get_query_args()
292+
local keys = {}
293+
for key, val in pairs(args) do
294+
table.insert(keys, key)
295+
end
296+
297+
table.sort(keys)
298+
for i, key in ipairs(keys) do
299+
ngx.say(key, " = ", args[key])
300+
end
301+
302+
ngx.say("updating args...")
303+
304+
ngx.var.args = "a=3&b=4"
305+
306+
local args = ngx.req.get_query_args()
307+
local keys = {}
308+
for key, val in pairs(args) do
309+
table.insert(keys, key)
310+
end
311+
312+
table.sort(keys)
313+
for i, key in ipairs(keys) do
314+
ngx.say(key, " = ", args[key])
315+
end
316+
317+
ngx.say("done")
318+
';
319+
}
320+
--- request
321+
GET /lua?foo=bar
322+
--- response_body
323+
foo = bar
324+
updating args...
325+
a = 3
326+
b = 4
327+
done
328+

0 commit comments

Comments
 (0)