Skip to main content

lcurl

lcurl manual


URIEncode and URIDecode

-- URL encoding/decoding examples
local curl = require('lcurl')
local e = curl.easy()
--
print(e:escape('abcd$%^&*()')) -- outputs "abcd%24%25%5E%26%2A%28%29"
--
print(e:unescape('abcd%24%25%5E%26%2A%28%29')) -- outputs "abcd$%^&*()"
--
sys.alert(print.out())

Note: Uses sys.alert

Example: simulate httpGet from TouchSprite

function httpGet(url) -- also works for ftp (this function is built-in; shown here only as lcurl usage)
if (url:sub(1, 6) ~= "ftp://" and
url:sub(1, 7) ~= "https://" and
url:sub(1, 8) ~= "https://") then
url = "https://"..url
end
local curl = require("curl.safe")
local buffer_t = {}
local write_f = function(s)
buffer_t[#buffer_t + 1] = s
end
local noerr, err = pcall(function()
curl.easy()
:setopt(curl.OPT_URL, url)
:setopt(curl.OPT_CONNECTTIMEOUT, 60)
:setopt_writefunction(write_f)
:perform()
end)
if (noerr) then
return table.concat(buffer_t)
else
return nil, err
end
end

Note: Uses table.concat