http.request

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.

http.request
Function
Syntax
http.request(
  • url : string
  • postData? : string
  • headers? : table
  • binary? : boolean
)

Returns nil
API http
Source CC:Tweaked (source) (source)

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.

ExampleMaking an asynchronous GET request.
Makes a GET request without blocking program flow.
Code
<nowiki>
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
    </nowiki>
Output Prints the HTML contents of http://example.com.
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.