Difference between revisions of "HTTP (API)"

From ComputerCraft Wiki
Jump to: navigation, search
m (Added types)
(Tidying, add return value column, add getResponseCode documentation)
Line 6: Line 6:
  
 
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
 
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
<tr><td colspan="2" style="font-weight: bold; font-size: large; padding-bottom: .3em; border-bottom: solid #C9C9C9 1px; background: #D3FFC2; line-height:28px;">
+
<tr><td colspan="3" style="font-weight: bold; font-size: large; padding-bottom: .3em; border-bottom: solid #C9C9C9 1px; background: #D3FFC2; line-height:28px;">
 
[[File:Grid_disk.png|24px]]&nbsp;&nbsp;
 
[[File:Grid_disk.png|24px]]&nbsp;&nbsp;
 
HTTP (API)
 
HTTP (API)
 
</td></tr>
 
</td></tr>
  
<tr><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
+
<tr><td style="width: 100px; background: #E0E0E0; padding: .4em; font-weight:bold;">Return</td><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[http.request]]( {{type|string}} url, {{type|string}} *postData )</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[nil]]</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[http.request]]({{type|string}} url, <nowiki>[</nowiki>{{type|string}} postData])</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP request to a website.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP request to a website.</td></tr>
  
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[http.get]]( {{type|string}} url )</td>
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{type|table}} handle</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[http.get]]({{type|string}} url)</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP GET request to a website.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP GET request to a website.</td></tr>
  
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[http.post]]( {{type|string}} url, {{type|string}} postData )</td>
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{type|table}} handle</td>
 +
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[http.post]]({{type|string}} url, {{type|string}} postData)</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP POST request to a website.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP POST request to a website.</td></tr>
 
</table>
 
</table>
  
A period of time after a http.request() call is made, a "http_success" or "http_failure" event will be raised to os.pullEvent(). Arguments are the URL and a file handle if successful. http.get() blocks until this event is fired.
+
[[http.request]] is used to send a HTTP request that completes asynchronously and generates an event (one of [[Http success (event)|http_success]] or [[Http failure (event)|http_failure]]). [[http.get]] and [[http.post]] execute [[http.request]] and block until the operation completes.
 +
 
 +
== Handles ==
 +
All three operations make use of ''handles'', tables that contain functions to read data returned from the HTTP server. These handles act the same as the I/O handles returned by [[fs.open]] in read-only text mode, implementing the [[fs.open#Closing_a_file_handle|close]], [[fs.open#Files_opened_in_text_read_mode|readLine]], and [[fs.open#Files_opened_in_text_read_mode|readAll]] methods. These handles also implement the following function:
 +
 
 +
{{Function
 +
|name=<var>h</var>.getResponseCode
 +
|returns={{type|number}} HTTP response code
 +
|api=HTTP
 +
|desc=Returns the numerical [https://en.wikipedia.org/wiki/List_of_HTTP_status_codes HTTP response code] sent by the server
 +
}}
  
 
[[Category:APIs]]
 
[[Category:APIs]]

Revision as of 22:59, 5 May 2013

The HTTP API must be enabled in mod_ComputerCraft.cfg before being used. To enable it open .minecraft/config/ComputerCraft.cfg and change enableAPI_http=false to enableAPI_http=true.

The HTTP API allows interfacing with websites and downloading from them.

Grid disk.png   HTTP (API)

ReturnMethod NameDescription
nil http.request(string url, [string postData]) Sends a HTTP request to a website.
table handle http.get(string url) Sends a HTTP GET request to a website.
table handle http.post(string url, string postData) Sends a HTTP POST request to a website.

http.request is used to send a HTTP request that completes asynchronously and generates an event (one of http_success or http_failure). http.get and http.post execute http.request and block until the operation completes.

Handles

All three operations make use of handles, tables that contain functions to read data returned from the HTTP server. These handles act the same as the I/O handles returned by fs.open in read-only text mode, implementing the close, readLine, and readAll methods. These handles also implement the following function:


Grid Redstone.png  Function h.getResponseCode
Returns the numerical HTTP response code sent by the server
Syntax h.getResponseCode()
Returns number HTTP response code
Part of ComputerCraft
API HTTP