http.get

From ComputerCraft
Jump to: navigation, search

http.get
Function
Syntax
http.get()
Returns table response | boolean false, string error[, table response]
API http
Source CC:Tweaked (source)

Attempt to fetch a webpage, using the same arguments as http.request. This will return the response table or false, an error message and (optionally) a handle with the failing response’s content.

The HTTP API assumes Unicode by default, meaning you may run into issues when fetching binary data. To avoid this, set binary to true. You will have to pass something to headers to set the binary flag, however just passing nil will work fine, e.g. http.get(url, nil, true).

ExampleExampleDownloading a page
Requests a single page and prints the contents.
Code
-- Make a HTTP request
local request, err = http.get("https://example.computercraft.cc")
if not request then error(err) end
-- Print the contents
print(request.readAll())
request.close() -- Don't forget to close!
Output Prints HTTP is working!
ExampleExampleDownloading binary data
Requests a binary page
Code
-- Make a HTTP request
local request, err = http.get("https://example.computercraft.cc/logo.gif", nil, true) -- Set binary mode to true
if not request then error(err) end
-- Write the binary data to the file
local file = fs.open("logo.gif", "wb") -- Open the file in binary mode
file.write(request.readAll())
file.close() -- Close the file to save it
request.close() -- Close the HTTP request too, now that we are done with it