Difference between revisions of "Http.request"

From ComputerCraft Wiki
Jump to: navigation, search
(Updated API reference)
m (Cleaned up the example, added a warning)
 
(5 intermediate revisions by 3 users not shown)
Line 2: Line 2:
 
{{Function
 
{{Function
 
|name=http.request
 
|name=http.request
|returns=event "http_success" on success.
+
|returns={{type|nil}}
 +
|args={{type|string}} url [, {{type|string}} postData [, {{type|table}} headers]]
 
|api=HTTP
 
|api=HTTP
 
|addon=ComputerCraft
 
|addon=ComputerCraft
|desc=Sends a HTTP request to the website. Returns event "http_success" on success.
+
|desc=Sends a HTTP request to a website, asynchronously. Returns immediately, with an [[Http success (event)|http_success]] or [[Http failure (event)|http_failure]] event being delivered later to indicate the outcome. Issues a POST request if ''postData'' is provided, or a GET request if it is [[nil]] (or omitted). <var>headers</var> '''must''' come as a third argument, if you need to make a GET request using custom headers just pass [[nil]] as the second argument.
 
|examples=
 
|examples=
 
{{Example
 
{{Example
|desc=Prints the code of a loading bar program if successful.
+
|desc=Prints the contents of ''example.com''.
|code=http.request("http://pastebin.com/raw.php?i=Tk19jv43")<br />
+
|code='''http.request("http://example.com")'''
  local requesting = true<br />
+
  while requesting do<br />
+
local requesting = true
    local event, url, sourceText = os.pullEvent()<br />
+
    if event == "http_success" then<br />
+
while requesting do
      local respondedText = sourceText.readAll()<br />
+
  local event, url, sourceText = [[os.pullEvent]]()
      print(respondedText)<br />
+
 
      requesting = false<br />
+
  if event == "http_success" then
    elseif event == "http_failure" then<br />
+
    local respondedText = sourceText.readAll()
      print("Server didn't respond.")<br />
+
   
      requesting = false<br />
+
    sourceText.close()
    end
+
    print(respondedText)
  end
+
   
 +
    requesting = false
 +
  elseif event == "http_failure" then
 +
    print("Server didn't respond.")
 +
   
 +
    requesting = false
 +
  end
 +
end
 
}}
 
}}
 
}}
 
}}
  
 
[[Category:Lua_Core_Functions]]
 
[[Category:Lua_Core_Functions]]

Latest revision as of 08:51, 6 June 2014


Grid Redstone.png  Function http.request
Sends a HTTP request to a website, asynchronously. Returns immediately, with an http_success or http_failure event being delivered later to indicate the outcome. Issues a POST request if postData is provided, or a GET request if it is nil (or omitted). headers must come as a third argument, if you need to make a GET request using custom headers just pass nil as the second argument.
Syntax http.request(string url [, string postData [, table headers]])
Returns nil
Part of ComputerCraft
API HTTP

Examples

Grid paper.png  Example
Prints the contents of example.com.
Code
http.request("http://example.com")

local requesting = true

while requesting do
  local event, url, sourceText = os.pullEvent()
  
  if event == "http_success" then
    local respondedText = sourceText.readAll()
    
    sourceText.close()
    print(respondedText)
    
    requesting = false
  elseif event == "http_failure" then
    print("Server didn't respond.")
    
    requesting = false
  end
end