http.request

From ComputerCraft
Jump to navigation Jump to search

http.request
Function
Syntax
http.request()
Returns nil
API http
Source CC:Tweaked (source) (source)

Asynchronously submit a HTTP GET request. Unlike http.get and http.post, this will not halt the program flow, which allows it to respond to other events while receiving the HTTP response.

If postData is specified, a POST request is submitted with the specified postData as the body.

Unlike http.get and http.post, the handle is provided via the http_success and http_failure events, which need to be listened for.

Error creating thumbnail: Unable to save thumbnail to destination
ExampleMaking an asynchronous GET request.
Makes a GET request without blocking program flow.
Code
http.request("http://example.com")

while true do
  local event, url, handle = os.pullEvent()

  if event == "http_failure" then
    printError("Failed to make HTTP GET request:")

    -- Here, handle is the error.
    printError(handle)
    break
  elseif event == "http_success" then
    print(handle.readAll())
    handle.close()
    break
  end
end
Output Prints the HTML contents of http://example.com.