The http
library can be used for sending HTTP(S) requests.
local http = require("http");
Create a simple asynchronous HTTP(S) GET request.
local url = "http://www.unifiedremote.com/whats-my-ip";
http.get(url, function (err, resp)
if (err) then return; end
print(resp);
end);
Create a simple asynchronous HTTP(S) POST request.
local url = "http://some.web.site/api/do";
local content = "some content to post";
http.post(url, content, function (err, resp)
if (err) then return; end
print(resp);
end);
Creates a custom HTTP(S) request specified by the request
table.
local headers = { "key" = "value",
"foo" = "bar" };
local req = { method = "post",
url = "http://foo.bar/api/do",
mime = "text/plain",
headers = headers,
content = "foobar" };
http.request(req, function (err, resp)
if (err) then return; end
print(resp);
end);
The response table looks like this:
{ status = "200",
reason = "ok",
length = "12345",
mime = "text/plain",
headers = {...},
content = "..." };
Scans the network for HTTP servers filtered by the specified options
.
-- Find all HTTP servers listening on port 81
local result = http.discover({ port = 81 });
Where result is an array of IP addresses:
{ "192.168.1.23",
"192.168.1.128" };
The options table supports the following values:
{ mask = "...", -- IP mask, default: 255.255.255.0
min = 123, -- min address, default: 0
max = 128, -- max address, default: 254
port = 81, -- port number, default: 80
timeout = 5 }; -- test timeout, default: 1 (sec)
HTML encode the specified value (escapes &<>"').
http.encode("&<>\"\'"); --> "&<>"'"
Decodes all HTML entities (named and numbered dec/hex) in the specified value.
http.decode("&''"); --> &''
URL encodes the specified value
.
http.urlencode("foo bar"); --> "foo%20bar"
Encodes URL component characters: ?#/=&:
http.urlcomponentencode("...");
URL decodes the specified value
.
http.urldecode("foo%20bar"); --> "foo bar"