-
Notifications
You must be signed in to change notification settings - Fork 187
Internet API
This library allows to simplify internet requests, to serialize Lua tables as POST-data, to perform URL-safe encoding, to instantly download or run files on HDD.
Contents |
---|
internet.request |
internet.download |
internet.encode |
internet.serialize |
internet.rawRequest |
internet.request(string url[, string postData, table headers, string method]): string or nil response, string reason
Performs an request to given url
. By default, request method
is GET, but if postData
was given and there's no specific method
, it switches to POST. If specific method was given, then the request method will be just that, regardless of the availability of a postData
. You can also pass optional headers
parameter with "User-Agent" or something else. Returns an string
server response on success, nil
and reason
message otherwise.
-- GET request
internet.request("http://httpbin.org/ip")
-- POST request
internet.request(
"http://httpbin.org/post",
"My POST data",
{
["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0"
},
)
-- HEAD request
internet.request(
"https://httpbin.org/headers",
nil,
nil,
"HEAD"
)
Downloads a file via given url
to given path
. Returns true
on success, false
and string
reason otherwise.
internet.download("http://example.com/file.txt", "/file.txt")
Returns an percent-encoding result of given string for URL-safe requests.
internet.encode("Hello, friend. My name is Игорь.")
> "Hello%2C%20friend.%20My%20name%20is%20%D0%98%D0%B3%D0%BE%D1%80%D1%8C."
Serializes an Lua table to URL query string and automatically encodes passed data via internet.encode().
internet.serialize({
array = {
string = "String test",
number = 512,
nestedArray = {
penis = "Vagina"
}
}
})
> "array[nestedArray][penis]=Vagina&array[number]=512&array[string]=String%20test"
internet.rawRequest(string url, string or nil postData, table or nil headers, function chunkHandler[, int chunkSize, string method]): boolean response, string reason
This method is used by internet.request() and internet.download(). It provides calling of chunkHandler
function on every downloaded chunk of data from server. Every chunkHandler
call happens with downloading of given chunkSize
bytes of data. Downloaded chunk
is being passed as argument to chunkHandler
.
For example, it's very useful for displaying something on screen of single-thread computers while processing large amount of data from server. Returns true
on successful finishing request, false
and reason
message otherwise.
internet.rawRequest(
"http://httpbin.org/user-agent",
nil,
nil,
function(chunk
-- Do something with downloaded chunk of data
end,
8
)
> "'user-ag"
> "ent':'Mo"
> "zilla/5."
> "0 (Macin"
> "tosh; In"
> "tel Mac "
...