HttpRequest Module API

The HttpRequest module implements a subset of the W3C XmlHttpRequest specification, and makes it available in both workers and the main HTML page.

Contents

  1. Overview
  2. Example
  3. HttpRequest class

Overview

Since the browser's XmlHttpRequest object is not available in the context of a worker, Gears provides its own HttpRequest object to fill that need. Gears HttpRequest provides most of the features of XmlHttpRequest except for the ability to access the response as an XML DOM object and the ability to send a request synchronously.

Permission

Does not require user permission.

Example

<script type="text/javascript" src="gears_init.js"></script>
<script type="text/javascript">
var request = google.gears.factory.create('beta.httprequest');
request.open('GET', '/index.html');
request.onreadystatechange = function() {
  if (request.readyState == 4) {
    console.write(request.responseText);
  }
};
request.send();
</script>

HttpRequest class

void     open(method, url)
void     setRequestHeader(name, value)
void     send([postData])
void     abort()
string   getResponseHeader(name)
string   getAllResponseHeaders()
callback onreadystatechange
readonly attribute int readyState
readonly attribute string responseText
readonly attribute int    status
readonly attribute string statusText 

Methods

abort()
Return value: void
Description: Cancels the request.
getAllResponseHeaders()
Return value: string
Description: Returns a string containing the entire set of HTTP headers in the server response.
getResponseHeader(headerName)
Return value: string
Parameters: headerName
Description: Returns the value of a specific HTTP header in the server response.
open(method, url)
Return value: void
Parameters: method
url
Description: Specifies the method and URL of a request.

The method parameter can have a value of "GET", "POST", "HEAD" or another HTTP method listed in the W3C specification.

The URL parameter may be either a relative or complete URL and must be from the same origin as the current context.

send([postData])
Return value: void
Parameters: postData - an optional string to be sent as the body of a POST or PUT request.
Description: Sends the request.
setRequestHeader(name, value)
Return value: void
Parameters: name - the HTTP header to set
value - the value of the header
Description: Adds the header to the set of HTTP headers to be sent.

Event Handler

callback onreadystatechange()
Description: An event handler that fires at every state change and repeatedly as new responseText data becomes available while the request is in the Interactive state.

Attributes

Attribute Type Description
readyState readonly int Returns the state of the object:
0 Uninitialized
1 Open
2 Sent
3 Interactive
4 Complete
responseText readonly string Returns the response body as a string. This property can be read when the request is in the Interactive or Complete state.
status readonly int Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK"). This property can be read when the request is in the Interactive or Complete state.
statusText readonly string Returns the status as a string (e.g. "Not Found" or "OK"). This property can be read when the request is in the Interactive or Complete state.