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.

  • url : string
  • headers? : table
  • binary? : boolean

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

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).

ExampleDownloading a page
Requests a single page and prints the contents.
-- Make a HTTP request
local request, err = http.get("")
if not request then error(err) end
-- Print the contents
request.close() -- Don't forget to close!
Output Prints HTTP is working!
ExampleDownloading binary data
Requests a binary page
-- Make a HTTP request
local request, err = http.get("", nil, true) -- Set binary mode to true
if not request then error(err) end
-- Write the binary data to the file
local file ="logo.gif", "wb") -- Open the file in binary mode
file.close() -- Close the file to save it
request.close() -- Close the HTTP request too, now that we are done with it
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.