Difference between revisions of "Http.request"

From ComputerCraft Wiki
Jump to: navigation, search
Line 2: Line 2:
 
{{Function
 
{{Function
 
|name=http.request
 
|name=http.request
|args={{type|string}} url<nowiki>[</nowiki>, {{type|string}} postData]
+
|args={{type|string}} url [, {{type|string}} postData [, {{type|table}} headers]]
 
|api=HTTP
 
|api=HTTP
 
|addon=ComputerCraft
 
|addon=ComputerCraft

Revision as of 19:54, 20 April 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).
Syntax http.request(string url [, string postData [, table headers]])
Returns nil
Part of ComputerCraft
API HTTP

Examples

Grid paper.png  Example
Prints "If you see this message, HTTP works!" if successful.
Code
http.request("http://pastebin.com/raw.php?i=itK2bx9K")
 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