Difference between revisions of "Http.request"

From ComputerCraft Wiki
Jump to: navigation, search
(Updated API reference)
(Document parameters, clarify description, remove return value (it DOES NOT return a value!), and remove excessive line breaks in example)
Line 2: Line 2:
 
{{Function
 
{{Function
 
|name=http.request
 
|name=http.request
|returns=event "http_success" on success.
+
|args={{type|string}} url<nowiki>[</nowiki>, {{type|string}} postData]
 
|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. 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).
 
|examples=
 
|examples=
 
{{Example
 
{{Example
 
|desc=Prints the code of a loading bar program if successful.
 
|desc=Prints the code of a loading bar program if successful.
|code=http.request("http://pastebin.com/raw.php?i=Tk19jv43")<br />
+
|code=http.request("http://pastebin.com/raw.php?i=Tk19jv43")
   local requesting = true<br />
+
   local requesting = true
   while requesting do<br />
+
   while requesting do
     local event, url, sourceText = os.pullEvent()<br />
+
     local event, url, sourceText = os.pullEvent()
     if event == "http_success" then<br />
+
     if event == "http_success" then
       local respondedText = sourceText.readAll()<br />
+
       local respondedText = sourceText.readAll()
       print(respondedText)<br />
+
       print(respondedText)
       requesting = false<br />
+
       requesting = false
     elseif event == "http_failure" then<br />
+
     elseif event == "http_failure" then
       print("Server didn't respond.")<br />
+
       print("Server didn't respond.")
       requesting = false<br />
+
       requesting = false
 
     end
 
     end
 
   end
 
   end

Revision as of 23:09, 5 May 2013


Grid Redstone.png  Function http.request
Sends a HTTP request to a website. 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).
Syntax http.request(string url[, string postData])
Returns nil
Part of ComputerCraft
API HTTP

Examples

Grid paper.png  Example
Prints the code of a loading bar program if successful.
Code
http.request("http://pastebin.com/raw.php?i=Tk19jv43")
 local requesting = true
 while requesting do
   local event, url, sourceText = os.pullEvent()
   if event == "http_success" then
     local respondedText = sourceText.readAll()
     print(respondedText)
     requesting = false
   elseif event == "http_failure" then
     print("Server didn't respond.")
     requesting = false
   end
 end