HTTP API

From ComputerCraft
Jump to: navigation, search
FunctionFunctionhttp.checkURL
Determine whether a URL is valid and is whitelisted, meaning the HTTP API can connect to it.
Syntax http.checkURL()
Returns boolean true | boolean false, string error
Part of CC:Tweaked (source)
API http
See also http.checkURLAsync
ExampleExampleCheck whether a URL can be visited
Tests whether a remote and local URL is whitelisted
Code
-- Check against a remote address
print(http.checkURL("https://example.computercraft.cc/"))
-- Check against a local address
print(http.checkURL("https://localhost"))
Output
true
false Domain not permitted


FunctionFunctionhttp.checkURLAsync
An asynchronous version of http.checkURL. Determines whether a URL is valid and is whitelisted, meaning the HTTP API can connect to it.
Syntax http.checkURLAsync()
Returns boolean true | boolean false, string error
Part of CC:Tweaked (source)
API http
See also http.checkURL
ExampleExampleCheck whether a URL can be visited
Tests whether a remote and local URL is whitelisted
Code
local checkURL = "https://example.computercraft.cc/"
local ok, err = http.checkURLAsync(checkURL)
if not ok then 
  printError(err)
else
  while true do
    local event, url, ok, err = os.pullEvent("http_check")
    if url == checkURL then
      if not ok then printError(err) end
      break
    end
  end
end
Output Nothing, as long as https://example.com.computercraft.cc is whitelisted.


FunctionFunctionhttp.get
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).
Syntax http.get()
Returns table response | boolean false, string error[, table response]
Part of CC:Tweaked (source)
API http
See also http.post, http.request
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


FunctionFunctionhttp.post
Attempt to fetch a webpage using the POST method, using the same arguments as http.request. This will return the response table or false, an error message and (sometimes) a handle with the failing response’s content (specifically, this is included when the actual request succeeded, but the server returned an erroneous HTTP code, e.g. 401: Unauthorized). 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 postData and headers to set the binary flag, e.g. http.post(url, "", nil, true).
Syntax http.post()
Returns table response | boolean false, string error[, table response]
Part of CC:Tweaked (source)
API http
See also http.get, http.request
ExampleExampleDownloading a page
Requests a single page with some postdata and prints the contents.
Code
-- Make a HTTP request
local request, err = http.post("https://example.computercraft.cc/post", "test=data&one=two")
if not request then error(err) end
-- Print the contents
print(request.readAll())
request.close() -- Don't forget to close!
Output Prints test=data&one=two

Http.request Http.websocket Http.websocketAsync