Module lcurl
Encodage et décodage en pourcentage d’URL (escape / unescape)
Mots-clés : encodage d’URL, encodage URI, encodage en pourcentage, URLEncode, URLDecode, URLEscape, PercentEscape
local curl = require('lcurl')
local e = curl.easy()
--
print(e:escape('abcd$%^&*()')) -- Sortie "abcd%24%25%5E%26%2A%28%29"
--
print(e:unescape('abcd%24%25%5E%26%2A%28%29')) -- Sortie "abcd$%^&*()"
--
sys.alert(print.out())
Remarque : le code ci-dessus utilise la fonction hors de ce chapitre sys.alert.
Exemple de httpGet basé sur lcurl
function httpGet(url) -- Peut aussi demander une ressource ftp (fonction déjà intégrée, démonstration de l’utilisation d’lcurl)
if (url:sub(1, 6) ~= "ftp://" and
url:sub(1, 8) ~= "https://" and
url:sub(1, 7) ~= "http://") 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
Remarque : le code ci-dessus utilise la fonction hors de ce chapitre table.concat.