HttpRequest API

The HttpRequest API 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. Interfaces
    1. HttpRequest
    2. HttpRequestUpload
    3. ProgressEvent

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>

Interfaces

HttpRequest class
    void open(method, url)
    void setRequestHeader(name, value)
    void send([postData])
    void abort()
    string getResponseHeader(name)
    string getAllResponseHeaders()
    callback onprogress(progressEvent)
    callback onreadystatechange()
    readonly attribute int readyState
    readonly attribute Blob responseBlob
    readonly attribute string responseText
    readonly attribute int status
    readonly attribute string statusText
    readonly attribute HttpRequestUpload upload
HttpRequestUpload class
    callback onprogress(progressEvent)
ProgressEvent class
    readonly attribute int total
    readonly attribute int loaded
    readonly attribute bool lengthComputable

HttpRequest class

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 or Blob 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 Handlers

callback onprogress(progressEvent)
Parameters: progressEvent - A ProgressEvent object.
Description: An event handler that fires as response data is downloaded.
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
responseBlob readonly Blob Returns the response body as a Blob. This property can be read when the request is in the Complete state.
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.
upload readonly Object Returns an HttpRequestUpload object for accessing properties associated with POST or PUT data uploads.

HttpRequestUpload class

Event Handlers

callback onprogress(progressEvent)
Parameters: progressEvent - A ProgressEvent object.
Description: An event handler that fires as PUT or POST data is uploaded.

ProgressEvent class

Attributes

Attribute Type Description
total readonly int Returns the total number of bytes to be loaded. Returns 0 if unknown (i.e. - lengthComputable is false).
loaded readonly int Returns the total number of bytes currently loaded.
lengthComputable readonly bool Returns true if the total number of bytes to be transferred is known.