HTTP
February 18, 2015 ยท View on GitHub
http
The http library can be used for sending HTTP(S) requests.
local http = require("http");
http.get( url, callback )
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);
http.post( url, content, callback )
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);
http.request( request, callback )
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 = "..." };
http.discover( options )
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)
encode/decode
http.encode( value )
HTML encode the specified value (escapes &<>"').
http.encode("&<>\"\'"); --> "&<>"'"
http.decode( value )
Decodes all HTML entities (named and numbered dec/hex) in the specified value.
http.decode("&''"); --> &''
http.urlencode( value )
URL encodes the specified value.
http.urlencode("foo bar"); --> "foo%20bar"
http.urlcomponentencode( value )
Encodes URL component characters: ?#/=&:
http.urlcomponentencode("...");
http.urldecode( value )
URL decodes the specified value.
http.urldecode("foo%20bar"); --> "foo bar"