When I use ngx.var.request_uri
I'm getting back a string that contains %20 in place of spaces. Is there a urldecode() function or similar to decode my string?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The decoded URI can be found in ngx.var.uri
. It does not contain the query string, if you need it see ngx.var.query_string
.
EDIT: if you cannot use this, here is a simple way to unescape a URL in Lua.
local hex_to_char = function(x)
return string.char(tonumber(x, 16))
end
local unescape = function(url)
return url:gsub("%%(%x%x)", hex_to_char)
end
Example usage:
local url = "/test/some%20string?foo=bar"
print(unescape(url)) -- /test/some string?foo=bar
But you should probably split the query string before using it.
回答2:
If you are using nginx-lua-module then you can use below api for this.
newstr = ngx.unescape_uri(str)
You can also take a look of ngxescape_uri