JavaScript API
JavaScript API
Time |
|||
|---|---|---|---|
|
Function |
Return Type |
Description |
Example |
|
delay(ms) |
N/A |
Pauses the program for ms. |
delay(1000); |
|
uptime() |
Number |
Returns the time since the device was started in seconds. |
Serial.println(uptime()); |
HTTP Client |
|||
|---|---|---|---|
|
Function |
Return Type |
Description |
Example |
|
new HTTPClient() |
HTTPClient |
Creates a HTTP Client. |
var http = new HTTPClient();
|
|
open(url) |
N/A |
Gets an URL. |
http.open(“http://www.cisco.com”); |
|
stop() |
N/A |
Stops the request. |
http.stop(); |
|
onDone |
N/A |
Sets the callback for when the request is done. |
http.onDone = function(status, data) { }; |
Real HTTP Server (External Network Access) |
|||
|---|---|---|---|
|
Function |
Return Type |
Description |
Example |
|
new RealHTTPServer() |
RealHTTPServer |
Creates a Real HTTP Server. |
var http = new RealHTTPServer();
|
|
start( port ) |
N/A |
Start server listening on port. |
http.start( 8765 ); |
|
stop( ) |
N/A |
Stop server listening. |
http.stop(); |
|
isListening( ) |
boolean |
Returns the listening status of the server. |
http.isListening( ); |
|
route( url-pattern, request-types, callback) |
N/A |
Adds callback to be run for all request-types matching url-pattern. callback: function (request, reply) url-pattern: string |
function
on_get_files( request, reply ){...} |
|
websocket( url-pattern, callback) |
N/A |
Adds callback be run for incoming websocket connections matching url-pattern. callback: function (client)
url-pattern: string |
function
callback( client ) {...}
|
|
|
|
|
|
|
RealHTTPServerRequest |
|
Request object passed to routing callbacks. |
|
|
headers( ) |
object |
Returns object containing request headers. |
var headers =
request.headers( ); |
|
method( ) |
string |
Returns request's method (“GET”, etc.) |
if(
request.method() == “POST” ) { |
|
body( ) |
string |
Returns the body of the request. |
request.body(); |
|
url( ) |
string |
Returns requested URL. |
request.url(); |
|
ip( ) |
string |
Returns IP address of the request origin. |
if( request.ip() == “1.1.1.1” ) ... |
|
|
|
|
|
|
RealHTTPServerResponse |
|
Response object passed to routing callbacks. |
|
|
setStatus( code ) |
N/A |
Sets response status to code. |
response.setCode( 200 ); // for OK response |
|
setHeaders( headers ) |
N/A |
Sets response headers in bulk. Object properties define header
names and values. |
response.setHeaders({ |
|
addHeader( name, value ) |
N/A |
Sets header name to
value. |
response.addHeader( “Cookie”, “Group=inner” ); |
|
appendContent( content ) |
N/A |
Appends content to response body. content: string |
response.appendContent( “more stuff“ ); |
|
resetContent( ) |
N/A |
Resets the body of the response to empty. |
response.resetContent(); |
|
end( ) |
N/A |
Initiates network transfer for the response. |
response.setContent(
“Hello, world!” ); response.setToPath(
“my-file.txt” ); |
|
setContent( content ) |
N/A |
Sets the body of the response to content. content: string |
response.setContent( “Hello, world!” ); |
|
setToFile( path ) |
N/A |
Sets content of the response to the contents of the file at path. The path must be local to the device running the server. path: string |
response.setToFile( “intro.html” ); |
|
setToNotFound( ) |
N/A |
Canned response for when a requested resource can not be located. |
response.setToNoFound(); |
|
send( content ) |
N/A |
Sends content as plain text. content: string |
response.send( “Hello, world!” ); |
|
setContentType( type ) |
N/A |
Sets Content-Type header to type. type: string |
response.setContentType( “text/html” ); |
|
sendFile( path ) |
N/A |
Sends the contents of the file at path. path: file path local to the device running the server. |
response.send( “my-file.txt” ); |
|
sendNotFound( ) |
N/A |
Sends canned response for when a requested resource can not be located. |
response.sendNotFound(); |
Real HTTP Client (External Network Access) |
|||
|---|---|---|---|
|
Function |
Return Type |
Description |
Example |
|
new RealHTTPClient() |
RealHTTPClient |
Creates a Real HTTP Client. |
var http = new RealHTTPClient();
|
|
get(url) |
N/A |
Gets an URL. |
http.get(“http://www.cisco.com”); |
|
post(url, data) |
N/A |
Posts data to an URL. data can be a string or a dictionary; if dictionary, it will be URL-encoded into the body. |
http.post(url, {"num":1, "str":"hello"}); |
|
put(url, data) |
N/A |
Puts data to an URL. data can be a string or a dictionary; if dictionary, it will be URL-encoded into the body. |
http.put(url, {"num":1, "str":"hello"}); |
|
deleteResource(url) |
N/A |
Sends a delete to an URL. |
http.deleteResource(url); |
|
getWithHeader(url, header) |
N/A |
Gets an URL with custom header fields as a dictionary. |
http.getWithHeader("https://api.ciscospark.com/v1/people/me",{"Authorization": "Bearer xxyyzz"}); |
|
postWithHeader(url, data, header) |
N/A |
Posts data to an URL with custom header fields as a dictionary. data can be a string or a dictionary; if dictionary and custom header field has "application/json" as the "content-type", it will be json-encoded, otherwise it will be URL-encoded into the body. |
http.postWithHeader("https://api.ciscospark.com/v1/messages",{"toPersonId": "722bb271-d7ca-4bce-a9e3-471e4412fa77", "text": "Hi Sparky"},{"Authorization": "Bearer xxyyzz", "Content-Type": "application/json"}); |
|
putWithHeader(url, data, header) |
N/A |
Puts data to an URL with custom header fields as a dictionary. data can be a string or a dictionary; if dictionary and custom header field has "application/json" as the "content-type", it will be json-encoded, otherwise it will be URL-encoded into the body. |
http.putWithHeader("https://api.ciscospark.com/v1/rooms/xxyyzz",{"title": "New room name"},{"Authorization": "Bearer xxyyzz"}); |
|
deleteResourceWithHeader(url, header) |
N/A |
Sends a delete to an URL with custom header fields as a dictionary. |
http.deleteResourceWithHeader("https://api.ciscospark.com/v1/messages/xxyyzz",{"Authorization": "Bearer xxyyzz"}); |
|
onDone |
N/A |
Sets the callback for when the request is done. replyHeader is a dictionary of header fields in the reply. It is added to 7.1 and is optional. |
http.onDone = function(status, data, replyHeader) { }; |
|
|
|
|
|
|
Short-hands without creating a RealHTTPClient |
|
|
|
|
RealHTTPClient.get(url, callback) |
N/A |
Gets an URL. |
RealHTTPClient.get("http://www.cisco.com",
function(status, data) { |
|
RealHTTPClient.post(url, data, callback) |
N/A |
Posts data to an URL. |
RealHTTPClient.post(url, {"num":1,
"str":"hello"}, function(status, data)
{ |
|
RealHTTPClient.put(url, data, callback) |
N/A |
Puts data to an URL. |
RealHTTPClient.put(url, {"num":1, "str":"hello"},
function(status, data) { |
|
RealHTTPClient.deleteResource(url, callback) |
N/A |
Sends a delete to an URL. |
RealHTTPClient.deleteResource(url, function(status, data) { |