HTTP

Fetch a resource over HTTP.

new HTTP("http://www.nidium.com/", function(ev) {
    console.log(ev.data);
});
var h = new HTTP("http://www.nidium.com/");

h.addEventListener("error", function(ev) {
    console.log("Error " + ev.errorCode + " : " + ev.error);
});

h.addEventListener("response", function(ev) {
    console.log("Received headers", JSON.stringify(ev.headers));
    console.log("Status code is ", ev.statusCode);
    console.log("Received data ", ev.data);
});

h.request({
    method: "GET",
    headers: {"foo": "bar"}
});

new HTTP(url[, params][, callback])

HTTP Object constructor.

If params and callback arguments are providen the HTTP request will be executed right away. Otherwise, you'll have to call the request method and listen for the response event.

Arguments:
url (String)

The url to fetch

params (Object)

object with details

method (String)

http method type POST, HEAD, GET, PUT, DELETE

headers (Object)

Object with http headers

timeout (Integer)

Timeout duration for the socket (in milliseconds)

maxRedirects (Integer)

Maximum number of redirects

data

(String | ArrayBuffer): The content (POST method only).

eval (Boolean)

Evaluate the data based on the content-type header for now only application/json and text/html are supported, other content-type are converted to an ArrayBuffer, `

path (String)

The requested path

followLocation (Boolean)

Follow HTTP redirect

callback (Function)

Function called on error or when the request is finished

event (Object)

Event Object or Error Event Object. See error and response event.

request([params])

Perform the HTTP request

Once a request is finished (response or error event has been fired), you can call the request method once again to run a new HTTP request. The options from the first request wil be reset.

Arguments:
params (Object)

object with details

method (String)

http method type POST, HEAD, GET, PUT, DELETE

headers (Object)

Object with http headers

timeout (Integer)

Timeout duration for the socket (in milliseconds)

maxRedirects (Integer)

Maximum number of redirects

data

(String | ArrayBuffer): The content (POST method only).

eval (Boolean)

Evaluate the data based on the content-type header for now only application/json and text/html are supported, other content-type are converted to an ArrayBuffer, `

path (String)

The requested path

followLocation (Boolean)

Follow HTTP redirect

Returns: HTTP

HTTP instance

stop()

Stop a pending request.

If a request is pending the error event will be fired.

Returns: HTTP

HTTP instance

Event: error(event)

Event called if an error occurs.

Parameters:
event (Object)

Error Event Object

error (String)

The error string.

errorCode (Integer)

The error code.

Event: headers(event)

Event called when all the headers has been received.

Parameters:
event (Object)

Event object with keys

headers (Object)

An object representing the response headers

statusCode (Integer)

HTTP status code

Event: progress(event)

Event called whenever new data is received (after the headers).

Parameters:
event (Object)

Event object

total (Integer)

Total bytes (retrieved from the content-length or if no content-length is providen 0)

read (Integer)

Number of bytes read so far

type (String)

The type of the data returned : String, json, binary

data

(null | Object | Object | String | ArrayBuffer): Response data

Event: response(event)

Event called when all the data has been read.

The received headers are analyzed and the value of 'event.data' and 'event.type' can be changed:

  • mimetype='application/octet-stream': event.type='binary' and event.data is set to an ArrayBuffer object.
  • mimetype='application/json': and valid data: event.type='json' and event.data is the parsed javascript value.
  • mimetype='application/json': and in valid data: event.type='string' and event.data is the original string.
  • mimetype='image/jpg' or 'image/png': event.type='image' and event.data is set to an Image object. (Frontend only).
Parameters:
event (Object)

Event object with keys

headers (Object)

An object representing the response headers

statusCode (Integer)

HTTP status code

data

(Void | Object | String | ArrayBuffer | Image): Response data

type (String)

The type of the data returned : String, json, binary


Caught a mistake or want to contribute to the documentation? Edit this page on GitHub!