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 property username)
- Keep state over requests
function onRefresh()
{
return Engines.KlipFood.process( Prefs.contentsource );
}
function onRefresh()
{
var request = Engines.HTTP.newRequest( Prefs.contentsource );
if (request.send() )
return Engines.KlipFood.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 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 messages in the HTTP header. Such request takes very little bandwidth.The following Klip demonstrates this optimization.
<klip>
<identity>
<title>
API: 304 Not Modified
</title>
</identity>
<locations>
<contentsource>
http://www.serence.com/support/samples/demo/demo-static.xml
</contentsource>
<icon>
http://www.serence.com/support/samples/images/sample_icon.png
</icon>
<banner>
http://www.serence.com/support/samples/images/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[
function onRefresh()
{
var request = Engines.HTTP.newRequest( Prefs.contentsource );
_t( "Access #1: " );
doHTTPRequest( request );
// - - This access will return a 304 Not Modified message
_t( "Access #2: " );
doHTTPRequest( request );
return true;
}
function doHTTPRequest( request )
{
if( request.send() )
{
if( request.response.data )
{
_t( "Got some data!" );
_t( request.response.headers );
// Add the data to the Klip
Engines.KlipFood.process( request.response.data );
}
}
else
{
_t( "Could not reach: " + request.url );
}
}
function _t( s )
{
trace( s + "\r\n" );
}
]]>
</klipscript>
</klip>
Access #1: Got some data! HTTP/1.1 200 OK Server: Zeus/4.3 Date: Thu, 31 Jul 2008 20:18:06 GMT Last-Modified: Tue, 10 Jun 2008 13:45:50 GMT Content-Type: text/xml Expires: Fri, 01 Aug 2008 20:18:06 GMT Content-Length: 721 Accept-Ranges: bytes Cache-Control: max-age=86400 Access #2: Got some data! HTTP/1.1 200 OK Server: Zeus/4.3 Date: Thu, 31 Jul 2008 20:18:07 GMT Last-Modified: Tue, 10 Jun 2008 13:45:50 GMT Content-Type: text/xml Expires: Fri, 01 Aug 2008 20:18:07 GMT Content-Length: 721 Accept-Ranges: bytes Cache-Control: max-age=86400
| 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 |
url
The URL to be processed. |
String |
username
Specifies a username for basic HTTP authentication. |
| Function Summary | |
boolean
|
post( <String> postdata)
Sends the HTTP request as a POST request, using postdata as the post data. |
boolean
|
send()
Sends the HTTP request. |
| 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.serence.com/support/samples/images/sample_icon.png
</icon>
<banner>
http://www.serence.com/support/samples/images/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.serence.com/sample_site/sources/sample_klip_icon_auth.gif";
var gIconNoAuth = "http://www.serence.com/sample_site/sources/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.serence.com/support/samples/images/api_login.png</img>" +
"</introitem>" +
"</klipfood>";
Items.clear( true );
Engines.KlipFood.stylesheet = g_intro_styles;
Engines.KlipFood.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.KlipFood.stylesheet == g_intro_styles )
{
Items.clear( true );
}
// - - Process incoming data with Klip's defined <style>
Engines.KlipFood.stylesheet = "@import klip";
Engines.KlipFood.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 messges and headers, see RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
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 |
post
boolean post( <String> postdata)
- Sends the HTTP request as a POST request, using postdata as the post data.
Note: Klipfolio Dashboard will only execute the post request if called from within an Klip.onRefresh() handler.
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: Klipfolio Dashboard will only execute the send request if called from within an Klip.onRefresh() handler.
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.
|
Klipfolio Dashboard 5.0 API | ||||||||
| PREV OBJECT NEXT OBJECT | FRAMES NO FRAMES | ||||||||
| SUMMARY: PROPERTY | FUNCTION | DETAIL: PROPERTY | FUNCTION | ||||||||
(c) 2008 Klipfolio Inc. All Rights Reserved.

