Difference between revisions of "Http success (event)"

From ComputerCraft Wiki
Jump to: navigation, search
(Created event page for http_success)
 
(Added some example code)
Line 5: Line 5:
 
|desc=Fired when a call to [[http.request|http.request()]], [[http.get|http.get()]] or [[http.post|http.post()]] completes successfully.
 
|desc=Fired when a call to [[http.request|http.request()]], [[http.get|http.get()]] or [[http.post|http.post()]] completes successfully.
 
|return1=The URL to the website.
 
|return1=The URL to the website.
|return2=A table containing functions to get the text of the request's reply.
+
|return2=A table containing functions of operations to perform on the requests reply
 +
}}
 +
{{Example
 +
|desc=Prints the response code from the server and the text returned from the server
 +
|code=
 +
http.request( "http://www.example.com" )
 +
event, url, handle = os.pullEvent( "http_success" )
 +
print( "Got a response with the status code: "..handle.getResponseCode() )
 +
print( "With the text: "..handle.readAll() )
 +
handle:close()
 +
}}
 +
{{Example
 +
|desc=Prints all the available functions to call on the response from the server
 +
|code=
 +
http.request( "http://www.example.com" )
 +
while true do
 +
  event, url, handle = os.pullEvent( "http_success" )
 +
  for k, v in pairs( handle ) do
 +
    print( k )
 +
  end
 +
end
 
}}
 
}}

Revision as of 18:13, 18 January 2013

This page needs some serious TLC, stat!
Please help us by cleaning it, fixing it up, or sparing it some love.
(Reason: A demonstration on the use and handling of this event would be beneficial. AfterLifeLochie 15:52, 30 November 2012 (MSK))


Grid Modem.png  Event http_success
Fired when a call to http.request(), http.get() or http.post() completes successfully.
Returned Object 1 The URL to the website.
Returned Object 2 A table containing functions of operations to perform on the requests reply


Grid paper.png  Example
Prints the response code from the server and the text returned from the server
Code
http.request( "http://www.example.com" )
event, url, handle = os.pullEvent( "http_success" )
print( "Got a response with the status code: "..handle.getResponseCode() )
print( "With the text: "..handle.readAll() )
handle:close()



Grid paper.png  Example
Prints all the available functions to call on the response from the server
Code
http.request( "http://www.example.com" )
while true do
  event, url, handle = os.pullEvent( "http_success" )
  for k, v in pairs( handle ) do
    print( k )
  end
end