Available since v0.4.0
local url = require "url"
url.encode(parts)
Encodes a URL from its components.
local str = url.encode {
scheme = "https",
username = "user",
password = "pass",
host = "example.com",
port = 443,
query = "key=value",
fragment = "section",
args = {
key1 = {"value1", "value2"},
key2 = {"value3"}
}
}
print(str)
-- "https://user:pass@example.com:443/?key=value&key1=value1&key1=value2&key2=value3#section"
NOTE: query
and args
are merged if both are provided.
Calling the package directly is an alias for url.encode
.
local str = url {
scheme = "https",
host = "example.com",
args = {
key = {"value"}
}
}
print(str)
-- "https://example.com?key=value"
url.decode(str)
Decodes a URL into its component parts.
local parts = url.decode "https://user:pass@example.com:443/path?key=value#section"
print(parts.scheme)
-- "https"
print(parts.host)
-- "example.com"
url.quote(table)
Serializes a Lua table into URL query parameters (application/x-www-form-urlencoded
).
local str = url.quote {
key = {"value1", "value2"},
other = {"value3"}
}
print(str)
-- "key=value1&key=value2&other=value3"
url.unquote(str)
Deserializes a query string into a Lua table.
local tbl = url.unquote "key=value1&key=value2&other=value3"
-- {
-- key = {
-- "value1",
-- "value2"
-- },
-- other = {
-- "value3"
-- }
-- }