Difference between revisions of "HTTP (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(It's not mod_ComputerCraft.cfg, it's ComputerCraft.cfg!)
Line 14: Line 14:
  
 
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">[[nil]]</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;">[[http.request]]({{type|string}} url [, {{type|string}} postData [, {{type|table}} headers]])</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP request to a website, asynchronously.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP request to a website, asynchronously.</td></tr>
  
 
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{type|table}} handle</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;">[[http.get]]({{type|string}} url [, {{type|table}} headers])</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP GET request to a website, synchronously.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP GET request to a website, synchronously.</td></tr>
  
 
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">{{type|table}} handle</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;">[[http.post]]({{type|string}} url, {{type|string}} postData [, {{type|table}} headers])</td>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP POST request to a website, synchronously.</td></tr>
 
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Sends a HTTP POST request to a website, synchronously.</td></tr>
 
</table>
 
</table>

Revision as of 19:52, 20 April 2014

The HTTP API must be enabled in ComputerCraft.cfg before being used. To enable it see this tutorial.

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 [, table headers]]) Sends a HTTP request to a website, asynchronously.
table handle http.get(string url [, table headers]) Sends a HTTP GET request to a website, synchronously.
table handle http.post(string url, string postData [, table headers]) Sends a HTTP POST request to a website, synchronously.

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