HTTP

From ComputerCraft
Jump to: navigation, search
EventEventhttp_check
Occurs after a successful call to http.checkURLAsync, containing information about whether the URL is blacklisted or not.
Returns
Part of CC:Tweaked
ExampleExamplePrints an error if blacklisted
Checks a URL and waits for a http_check event, printing an error if it is blacklisted.
Code
local check_url = "https://example.computercraft.cc/"
local ok, err = http.checkURLAsync(check_url)
if not ok then 
  printError(err)
else
  while true do
    local event, url, ok, err = os.pullEvent("http_check")
    if url == check_url then
      if not ok then printError(err) end
      break
    end
  end
end
EventEventhttp_failure
Fired when a request created with http.request fails.
Returns
  • url : string The URL of the request
  • reason : string The reason the request failed. Most often "Could not connect."
  • stream? : table A file handle-like object. If the connection failed, this will not be returned.
Part of CC:Tweaked
ExampleExampleRequest a page
Requests the example 404 page and prints the result.
Code
-- Request the example page
http.request("https://example.computercraft.cc/404")
while true do
  -- Wait for a http_failure event.
  local event, url, handle = os.pullEvent("http_failure")
  -- If it's for our URL, then print the result.
  if url == "https://example.computercraft.cc/404" then
    print(reason, handle and handle.readAll() or "")
    if handle then handle.close() end
  end
end
EventEventhttp_success
Occurs when a HTTP request has completed successfully. Fired by a http.request call.
Returns
  • url : string The URL of the request
  • handle : table A file-handle-like object that contains the contents of the request.
Part of CC:Tweaked
ExampleExampleRequest a page
Requests the example page and prints the result.
Code
-- Request the example page
http.request("https://example.computercraft.cc")
while true do
  -- Wait for a http_success event.
  local event, url, handle = os.pullEvent("http_success")
  -- If it's for our URL, then print the result.
  if url == "https://example.computercraft.cc" then
    print(handle.readAll())
    handle.close()
  end
end