Skip to content

Commit f94c18c

Browse files
StarlightIbukipintsized
authored andcommitted
fix: no trailing "?" for empty query table
1 parent d4eaf88 commit f94c18c

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

lib/resty/http.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,10 @@ local function _format_request(self, params)
313313

314314
local query = params.query or ""
315315
if type(query) == "table" then
316-
query = "?" .. ngx_encode_args(query)
317-
elseif query ~= "" and str_sub(query, 1, 1) ~= "?" then
316+
query = ngx_encode_args(query)
317+
end
318+
319+
if query ~= "" and str_sub(query, 1, 1) ~= "?" then
318320
query = "?" .. query
319321
end
320322

t/01-basic.t

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,45 @@ GET /a
389389
--- no_error_log
390390
[error]
391391
[warn]
392+
393+
394+
395+
=== TEST 14: Empty query
396+
--- http_config eval: $::HttpConfig
397+
--- config
398+
location = /a {
399+
content_by_lua '
400+
local http = require "resty.http"
401+
local httpc = http.new()
402+
httpc:connect{
403+
scheme = "http",
404+
host = "127.0.0.1",
405+
port = ngx.var.server_port
406+
}
407+
408+
local res, err = httpc:request{
409+
query = {},
410+
path = "/b"
411+
}
412+
413+
ngx.status = res.status
414+
415+
ngx.print(ngx.header.test)
416+
417+
httpc:close()
418+
';
419+
}
420+
location = /b {
421+
content_by_lua '
422+
ngx.header.test = ngx.var.request_uri
423+
';
424+
}
425+
--- request
426+
GET /a
427+
--- response_headers
428+
/b
429+
--- no_error_log
430+
[error]
431+
[warn]
432+
433+

0 commit comments

Comments
 (0)