Internet API - ProfessorCrown/WIKI-RUSSIA GitHub Wiki
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 or nil response, string reason
Performs an request to given url. By default, request method is GET, but if postData was given, it switches to POST. You can also pass optional headers if desired. Returns an string server response on success, nil and reason message otherwise.
internet.request("http://httpbin.org/ip")
internet.request("http://httpbin.org/post", "My POST data", {
["My header name"] = "My header value"
})
internet.download(string url, string path): boolean success, string reason
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")
internet.encode(string data): string result
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."
internet.serialize(table data): string result
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): 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 "
...