Difference between revisions of "HTTP (API)"

From ComputerCraft Wiki
Jump to: navigation, search
(Add http.checkURL method)
m (fixed formatting)
 
Line 27: Line 27:
 
{{API table/row
 
{{API table/row
 
|[[http.checkURL]]({{type|string}} url)
 
|[[http.checkURL]]({{type|string}} url)
|{{type|boolean}} sucess, {{type|string}} message
+
|{{type|boolean}} success [<nowiki />, {{type|string}} error]
|Checks if a url is valid and is whitelisted
+
|Checks if a URL is valid and is included in the HTTP whitelist.
|odd}}
+
|}}
  
 
}}
 
}}
Line 49: Line 49:
 
{{Example
 
{{Example
 
|desc=Sends a request to http://example.com/ with the custom headers.
 
|desc=Sends a request to http://example.com/ with the custom headers.
|code=<nowiki>local headers = {
+
|code=local headers = {
  ["User-Agent"] = "A custom user-agent!", -- Overrides the default User-Agent.
+
  [ "User-Agent" ] = "A custom user-agent!", ''-- Overrides the default User-Agent.''
  ["Hi"] = "Hello" -- A non-standard custom header field.
+
  [ "Hi" ] = "Hello" ''-- A non-standard custom header field.''
}
+
}
http.get("http://example.com/", headers)</nowiki>
+
 +
[[http.get]]( "http:<nowiki/>//example.com/", headers )
 
}}
 
}}
  
 
[[Category:APIs]]
 
[[Category:APIs]]

Latest revision as of 20:35, 1 August 2015

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)
Function Return values Description
http.request(string url [, string postData [, table headers]]) nil Sends a HTTP request to a website, asynchronously.
http.get(string url [, table headers]) table handle Sends a HTTP GET request to a website, synchronously.
http.post(string url, string postData [, table headers]) table handle Sends a HTTP POST request to a website, synchronously.
http.checkURL(string url) boolean success [, string error] Checks if a URL is valid and is included in the HTTP whitelist.

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


Headers

As of ComputerCraft 1.63, you can optionally set custom headers. This also means that you can override default headers such as the User-Agent.

Grid paper.png  Example
Sends a request to http://example.com/ with the custom headers.
Code
local headers = {
  [ "User-Agent" ] = "A custom user-agent!", -- Overrides the default User-Agent.
  [ "Hi" ] = "Hello" -- A non-standard custom header field.
}

http.get( "http://example.com/", headers )