The HTTPRequest object gives you fine-grain control over the HTTP request. This control lets you
- Enable/Disable sending of timestamp
- Create your own headers
- Provide username and password for Basic User Authentication (see password() for an example)
- Keep state over requests
function onRefresh()
{
return Engines.Data.process( Prefs.contentsource );
}
function onRefresh()
{
var request = Engines.HTTP.newRequest( Prefs.contentsource );
if (request.send() )
return Engines.Data.process( request.response.data );
else
return false;
}
Creating your own HTTPRequest object, as shown above, lets you reuse an Engines.HTTP.HTTPRequest object for multiple requests, each time modifying its properties and calling send() function again.
Enabling/Disabling Sending of Timestamp
Klipfolio Dashboard automatically optimizes an HTTP request by sending the timestamp of the last successful request (which it remembers) in the request header. This enables the remote web server to determine if the requested data has changed since the last request and, if not, responds with an empty body the text HTTP 304 Not Modified in the HTTP header. Such request takes very little bandwidth.The following Klip demonstrates this optimization. Load it into your Klipfolio Dashboard and see its output in the Debug Window. Then, refresh it again to see that the response header now includes HTTP 304 Not Modified if the data on the server has not changed.
<klip>
<identity>
<title>
API: 304 Not Modified
</title>
</identity>
<locations>
<contentsource>
http://support.klipfolio.com/files/demo/demo.xml
</contentsource>
<icon>
http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
</icon>
<banner>
http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png
</banner>
</locations>
<style>
alert {
type: item;
}
sender {
itemcol: 2;
noterow: 1;
label: "Sender";
emphasis: strong;
}
summary {
itemcol: 3;
noterow: 4;
wrap: false;
notelabel: false;
}
date {
itemcol: 4;
noterow: 2;
format: "nolocaltime";
label: "Date";
}
category {
noterow: 3;
label: "Category";
}
level {
itemcol: 1;
noterow: 5;
notelabel: false;
type: image;
}
url {
type: link;
}
id {
key: override;
}
</style>
<klipscript>
<![CDATA[
function onRefresh() {
traceln( "---------------------- Refreshing" );
var request = Engines.HTTP.newRequest( Prefs.contentsource );
doHTTPRequest( request );
return true;
}
function doHTTPRequest( request ) {
// If the HTTP request is sent successfully
if ( request.send() ) {
// And if a new/changed set of data is returned by the server
if ( request.response.data ) {
traceln( request.response.headers );
// Add the data to the Klip
Engines.Data.process( request.response.data );
}
// If the data hasn't changed since the last request
else {
traceln( request.response.headers );
}
}
else {
traceln( "---------------------- Could not reach: " + request.url );
}
}
]]>
</klipscript>
</klip>
You can disable this optimization, thus ensuring that you always get a response, by setting the
sendtimestamp property to false before sending the request. For more
information about
304 messages and headers, see RFC 2616: Hypertext Transfer
Protocol --
HTTP/1.1
| Properties Summary | |
Engines.HTTP.HTTPResponse |
acceptgzip
Specifies the HTTP request can accept gzip compress response (default false). |
boolean |
autoredirect
Specifies whether Klipfolio Dashboard should follow HTTP redirects (such as 302 Moved Permanently). |
Engines.HTTP.HTTPResponse |
binary
Specifies that response received from the HTTP request stored in the data parameter should be treated as binary (default false). |
Engines.HTTP.HTTPResponse |
binarypost
Specifies that the POST data should treat double-byte Unicode characters as single-byte characters. |
boolean |
cached
Specifies whether Klipfolio Dashboard should try to return a cached copy of the data (if one exists) and store a copy of the data keyed to the specified request URL. |
String |
cookie
Specifies the cookies to be sent with the request. |
String |
headers
Specifies HTTP headers to be sent with the request. |
integer |
maxsize
Specifies the maximum amount of data Klipfolio Dashboard will download from the server before terminating its connection. |
String |
method
Specifies the HTTP method to be used by the request, typically one of GET (default), HEAD, or POST, but may be an arbitrary method such as PROPFIND for WebDAV. |
String |
password
Specifies a password for basic HTTP authentication. |
String |
postdata
Specifies data to be sent with the request following headers for POST requests. |
String |
referer
Specifies the Referer: header sent with the request. |
Engines.HTTP.HTTPResponse |
response
Returns the Engines.HTTP.HTTPResponse object created after the call to send(). |
boolean |
sendtimestamp
Specifies whether Klipfolio Dashboard sends an If-Modified-Since header for this request (default true). |
String |
state
A read-only string containing "connected", "connecting" or "disconnected" representing the current state of an asynchronous HTTP connection. |
integer |
timeout
Specifies the amount of time to wait for the request to return, in seconds. |
String |
url
The URL to be processed. |
String |
username
Specifies a username for basic HTTP authentication. |
| Function Summary | |
function
|
onData()
A function callback handler for receiving data (partial or complete) as a string from an asynchronous HTTP connection. |
function
|
onStateChange()
A function callback handler for receiving notifications of changes to the state property of an asynchronous HTTP connection as a string containing "connected", "connecting" or "disconnected". |
boolean
|
post( <String> postdata)
Sends the HTTP request as a POST request, using postdata as the post data. |
boolean
|
send()
Sends the HTTP request. |
boolean
|
sendAsync()
Sends the HTTP request asynchronously. |
| Properties Detail |
acceptgzip
Engines.HTTP.HTTPResponse acceptgzip
-
Specifies the HTTP request can accept gzip compress response (default false).
Set to true to include the header "Accept: gzip" in your request.
autoredirect
boolean autoredirect
-
Specifies whether Klipfolio Dashboard should follow HTTP redirects (such as 302 Moved Permanently).
If Klipfolio Dashboard follows one or more HTTP redirects, the Engines.HTTP.HTTPResponse object will contain the final destination URL. Default value is true.
binary
Engines.HTTP.HTTPResponse binary
-
Specifies that response received from the HTTP request stored in the data parameter should be treated as binary (default
false).
Setting to true prevents Klipfolio Dashboard from attempting to transcode the data downloaded into your Klip's codepage.
binarypost
Engines.HTTP.HTTPResponse binarypost
-
Specifies that the POST data should treat double-byte Unicode characters as single-byte characters.
Use this when uploading any binary data through HTTP Post request, such as a photo to Flickr.
cached
boolean cached
-
Specifies whether Klipfolio Dashboard should try to return a cached copy of the data (if one exists) and store a copy of the
data keyed to
the specified request
URL.
Subsequent HTTP requests to the same URL with cached == true will not result in network connections (unless the cache was cleared).
cookie
String cookie
-
Specifies the cookies to be sent with the request.
This header will be merged with any other headers specified. This is a shorthand for using the headers property to specify the "Cookie:" header.
headers
String headers
-
Specifies HTTP headers to be sent with the request.
Note
If a cookie is specified in the user-defined headers, it overrides the settings cookie() property.
maxsize
integer maxsize
-
Specifies the maximum amount of data Klipfolio Dashboard will download from the server before terminating its connection.
This is a "brute force" option to allow more flexibility in Klips using HTTP 1.0 data sources. Note: For data sources larger than 8,192 bytes, a minimum of 8,192 bytes will be downloaded due to TCP/IP optimizations.
If your data source uses a proper implementation of HTTP 1.1, it is best to use the "Range:" HTTP header to limit download size.
method
String method
-
Specifies the HTTP method to be used by the request, typically one of GET (default), HEAD, or
POST, but may be an arbitrary method such as PROPFIND for WebDAV.
password
String password
-
Specifies a password for basic HTTP authentication.
Overrides username specified in URLs http://user:pass@host.domain.
Example: Basic User Authentication
Like a browser, Klipfolio Dashboard can authenticate itself to a site that requests Basic User Authentication.
For example, if you access the following URL http://www.serence.com/working/protected/test.food, your browser will prompt you to authenticate (usename: pumpkin, password: pie).
The following Klip shows how a Klip can ask the user for a username and password to authenticate against a site that is protecting a directory using Basic User Authentication.
<klip>
<identity>
<title>
API: HTTPRequest
</title>
</identity>
<locations>
<contentsource>
http://www.serence.com/working/protected/test.xml
</contentsource>
<icon>
http://www.klipfolio.com/static/klips/klipfolio/sample_icon.png
</icon>
<banner>
http://www.klipfolio.com/static/klips/klipfolio/sample_banner.png
</banner>
</locations>
<style>
alert {
type: item;
}
sender {
itemcol: 2;
noterow: 1;
label: 'Sender';
emphasis: strong;
}
summary {
itemcol: 3;
noterow: 4;
wrap: false;
notelabel: false;
}
date {
itemcol: 4;
noterow: 2;
label: 'Date';
}
category {
noterow: 3;
label: 'Category';
}
level {
itemcol: 1;
noterow: 5;
notelabel: false;
type: image;
}
url {
type: link;
}
id {
key: override;
}
</style>
<klipscript>
<![CDATA[
// - - - Intro text if the user has not connected.
var g_intro_styles =
"introitem {type:item;}" +
"introtext {itemcol:1; wrap:false;}" +
"introimages {itemcol:2; type:image; height:3;}" +
"helpheader {noterow:1; notelabel:false; emphasis:strong;}" +
"helptext {noterow:2; notelabel:false;}" +
"img {noterow:3; type:image; notelabel:false;}";
var gUserNameField, gPasswordField;
var gIconAuth = "http://www.klipfolio.com/static/klips/devguide/sample_klip_icon_auth.gif";
var gIconNoAuth = "http://www.klipfolio.com/static/klips/devguide/sample_klip_icon_noauth.gif";
function onLoad()
{
show_state();
var tab = Setup.addTab( "Authentication" );
tab.addText("User name:");
gUserNameField = tab.addTextField();
tab.addText("Password:");
gPasswordField = tab.addTextField();
gPasswordField.obscured = true;
if (Prefs.getPref("UserName") != "" ) {
gUserNameField.value = Prefs.getPref("UserName");
}
if (Prefs.getPref("Password") != "" ) {
gPasswordField.value = Prefs.getPref("Password");
}
// - - update prefs when the user closes the setup
Setup.onClose = savePrefs;
// - - Put a welcome message in the Klip
if( ! gUserNameField.value ) {
setStatus( "Click here to login" );
}
}
function clickWelcomeMessage ()
{
Setup.open(0);
}
function savePrefs ()
{
Prefs.setPref( "UserName", gUserNameField.value );
Prefs.setPref( "Password", gPasswordField.value );
}
function setStatus ( str )
{
var g_intro_text =
"<klipfood>" +
"<introitem>" +
"<introtext>" + str + "</introtext>" +
"<helpheader>You need to login to access remote content</helpheader>" +
"<helptext>Use 'pumpkin' for Username, 'pie' for Password.</helptext>" +
"<img>http://www.klipfolio.com/static/klips/devguide/api_login.png</img>" +
"</introitem>" +
"</klipfood>";
Items.clear( true );
Engines.Data.stylesheet = g_intro_styles;
Engines.Data.process( g_intro_text )
Items[0].candelete = false;
Items[0].canvisit = false;
Items[0].canautoremove = false;
Items[0].onClick = clickWelcomeMessage;
}
function onRefresh()
{
if( ! gUserNameField.value ) {
_t( "no password" );
return false;
}
_t( "Attempting to connect\r\n username: " + gUserNameField.value
+ "\r\n password: " + gPasswordField.value + "\r\n" );
var req = Engines.HTTP.newRequest( Prefs.contentsource );
// - - Assign the username and password to the HTTPRequest object
req.username = gUserNameField.value; // Set username
req.password = gPasswordField.value; // Set password
var result = req.send();
if ( result )
{
_t( "Headers: " + req.response.headers );
_t( "Headers: " + req.response.data );
Items.icon = gIconAuth;
// - Clear status message if using intro sytlesheet
if (Engines.Data.stylesheet == g_intro_styles )
{
Items.clear( true );
}
// - - Process incoming data with Klip's defined <style>
Engines.Data.stylesheet = "@import klip";
Engines.Data.process( req.response.data );
}
else
{
if ( req.response.status == 401 )
{
_t( "Bad username/password (result: " + result + "), response: "
+ req.response.headers + " status:" + req.response.status);
Items.icon = gIconNoAuth;
setStatus( "[login failed]" );
}
else
{
_t( "Unable to login (result: " + result + "), response: "
+ req.response.headers + " status:" + req.response.status);
setStatus( "[unable to login]" );
}
}
return result;
}
//
// Useful Debug Code -----------------------------------------------
//
var button;
function show_state()
{
show_all_prefs ();
}
function show_all_prefs()
{
if (Prefs.length == 0)
{
trace ("There are no Prefs defined\r\n");
return;
}
trace( "------------------------\r\n" );
for (var i = 0; i < Prefs.length; i++)
{
trace("Prefs.getPref( \""+Prefs[i].name+"\" ) == \""+Prefs[i].value+"\"\r\n");
}
trace( "------------------------\r\n" );
}
var gTrace = true;
function _t( s )
{
if ( gTrace ) {
trace( s + "\r\n" );
}
}
]]>
</klipscript>
</klip>
req.username = gUserNameField.value; // Set username
req.password = gPasswordField.value; // Set password
postdata
String postdata
-
Specifies data to be sent with the request following headers for POST requests.
referer
String referer
-
Specifies the Referer: header sent with the request.
This header will be merged with any other headers specified. This is a shorthand for using headers property to specify the "Referer:" header.
response
Engines.HTTP.HTTPResponse response
-
Returns the Engines.HTTP.HTTPResponse object created after the call to send().
sendtimestamp
boolean sendtimestamp
-
Specifies whether Klipfolio Dashboard sends an If-Modified-Since header for this request (default true).
If false, the web server will always send the full contents of the request.
For more information about 304 messages and headers, see RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
state
String state
-
A read-only string containing "connected", "connecting" or "disconnected" representing the current state of an asynchronous
HTTP
connection.
Since:
5.3
timeout
integer timeout
-
Specifies the amount of time to wait for the request to return, in seconds. Default is 15 seconds.
url
String url
-
The URL to be processed.
This property contains the URL specified in the URL parameter to Engines.HTTP.newRequest().
username
String username
-
Specifies a username for basic HTTP authentication.
Overrides username specified in URLs http://user:pass@host.domain.
| Function Detail |
onData
function onData()
- A function callback handler for receiving data (partial or complete) as a string
from an
asynchronous HTTP connection.
Note that this callback is always in the scope of the HTTPRequest object whose onData handler is set.
Use this.xxx to access properties of the original HTTPRequest object, e.g. this.response.data. There is actually no data available in the scope of onData from this.response.data. Instead, it is passed as a parameter to onData.
Since:
5.3
onStateChange
function onStateChange()
- A function callback handler for receiving notifications of changes to the state
property of an asynchronous HTTP connection as a string
containing "connected", "connecting" or "disconnected".
Since:
5.3
post
boolean post( <String> postdata)
- Sends the HTTP request as a POST request, using postdata as the post data.
Note: HTTP requests should only be made from Actions.Window.onNext() or Klip.onRefresh() handlers.
The following is a table that specifies the return values from post() based on the HTTP status code returned by the server:
| Code | Meaning | Returns | Notes |
|---|---|---|---|
| 100 | OK to continue with request | true | |
| 101 | server has switched protocols in upgrade header | true | |
| 200 | request completed | true | |
| 201 | object created, reason = new URI | true | |
| 202 | async completion (TBS) | true | |
| 203 | partial completion | true | |
| 204 | no info to return | true | |
| 205 | request completed, but clear form | true | |
| 206 | partial GET fulfilled | true | |
| 300 | server couldn't decide what to return | true | |
| 301 | object permanently moved | true | Will follow Location header if autoredirect is on |
| 302 | object temporarily moved | true | Will follow Location header if autoredirect is on |
| 303 | redirection w/ new access method | true | |
| 304 | if-modified-since was not modified | true | |
| 305 | redirection to proxy, location header specifies proxy to use | true | |
| 307 | HTTP/1.1: keep same verb | true | |
| 400 | invalid syntax | false | |
| 401 | access denied | false | |
| 402 | payment required | false | |
| 403 | request forbidden | false | |
| 404 | object not found | false | |
| 405 | method is not allowed | false | |
| 406 | no response acceptable to client found | false | |
| 407 | proxy authentication required | true | Will return false if authorization fails. |
| 408 | server timed out waiting for request | false | |
| 409 | user should resubmit with more info | false | |
| 410 | the resource is no longer available | false | |
| 411 | the server refused to accept request w/o a length | false | |
| 412 | precondition given in request failed | false | |
| 413 | request entity was too large | false | |
| 414 | request URI too long | false | |
| 415 | unsupported media type | false | |
| 449 | retry after doing the appropriate action. | false | |
| 500 | internal server error | false | |
| 501 | required not supported | false | |
| 502 | error response received from gateway | false | |
| 503 | temporarily overloaded | false | |
| 504 | timed out waiting for gateway | false | |
| 505 | HTTP version not supported | false |
-
Parameters:
postdata - data to be sent with the request following headers for POST requests
-
Returns:
-
true if the data was successfully posted; otherwise false. .
send
boolean send()
- Sends the HTTP request.
The request will automatically detect 304 Not Modified messages and follow any autoredirects.
As Klipfolio Dashboard follows the HTTP redirections, it will accumulate and re-transmit cookies as they are received by each
step of the
redirection.
Note: HTTP requests should only be made from Actions.Window.onNext() or Klip.onRefresh() handlers.
The following is a table that specifies the return values from send() based on the HTTP status code returned by the server:
| Code | Meaning | Returns | Notes |
|---|---|---|---|
| 100 | OK to continue with request | true | |
| 101 | server has switched protocols in upgrade header | true | |
| 200 | request completed | true | |
| 201 | object created, reason = new URI | true | |
| 202 | async completion (TBS) | true | |
| 203 | partial completion | true | |
| 204 | no info to return | true | |
| 205 | request completed, but clear form | true | |
| 206 | partial GET fulfilled | true | |
| 300 | server couldn't decide what to return | true | |
| 301 | object permanently moved | true | Will follow Location header if autoredirect is on |
| 302 | object temporarily moved | true | Will follow Location header if autoredirect is on |
| 303 | redirection w/ new access method | true | |
| 304 | if-modified-since was not modified | true | |
| 305 | redirection to proxy, location header specifies proxy to use | true | |
| 307 | HTTP/1.1: keep same verb | true | |
| 400 | invalid syntax | false | |
| 401 | access denied | false | |
| 402 | payment required | false | |
| 403 | request forbidden | false | |
| 404 | object not found | false | |
| 405 | method is not allowed | false | |
| 406 | no response acceptable to client found | false | |
| 407 | proxy authentication required | true | Will return false if authorization fails. |
| 408 | server timed out waiting for request | false | |
| 409 | user should resubmit with more info | false | |
| 410 | the resource is no longer available | false | |
| 411 | the server refused to accept request w/o a length | false | |
| 412 | precondition given in request failed | false | |
| 413 | request entity was too large | false | |
| 414 | request URI too long | false | |
| 415 | unsupported media type | false | |
| 449 | retry after doing the appropriate action. | false | |
| 500 | internal server error | false | |
| 501 | required not supported | false | |
| 502 | error response received from gateway | false | |
| 503 | temporarily overloaded | false | |
| 504 | timed out waiting for gateway | false | |
| 505 | HTTP version not supported | false |
-
Returns:
-
true if the data was successfully sent and a server response was obtained, otherwise returns false. Note that a true does not mean that Engines.HTTP.HTTPResponse.data contains data, but rather that server accepted the HTTP request and supplied a valid response, such as HTTP 404 File Not Found.
sendAsync
boolean sendAsync()
- Sends the HTTP request asynchronously. The connection is left open and the data source is continuously
monitored, and a callback is generated when the remote data has changed.
Since:
5.3
-
Returns:
-
true if the data was successfully sent and a server response was obtained; otherwise, false. Note that a true does not mean that Engines.HTTP.HTTPResponse.data contains data, but rather that the server accepted the HTTP request and supplied a valid response, such as HTTP 404 File Not Found.
|
Klipfolio Dashboard API | ||||||||
| PREV OBJECT NEXT OBJECT | FRAMES NO FRAMES | ||||||||
| SUMMARY: PROPERTY | FUNCTION | DETAIL: PROPERTY | FUNCTION | ||||||||
© 2011 Klipfolio Inc. All Rights Reserved.
