JavaScript Documentation / http

This object contains methods for performing HTTP requests.

http.request(options, [callback])

Perform a HTTP request.

Parameters

  • options required A set of key/value pairs that configure the request.
    • url The URL to which the request is sent.
    • type The type of the request to make. Valid values are GET, POST, PUT, PATCH and DELETE. Default is GET.
    • data The data to be sent to the server.
    • headers An object of additional header key/value pairs to send along with the request.
    • username A username to be used with the request in response to an HTTP access authentication request.
    • password A password to be used with the request in response to an HTTP access authentication request.
  • callback optional The function to execute when the request completes. This function gets passed two arguments:
    • data The data returned from the server.
    • error An array contains status code and error message describing the error, if one occurred.

Example

http.request({
    url: 'http://httpbin.org/get',
    data: { 'foo': 'bar' }
}, function(data) {
    ui.alert(data.args.foo);
});

http.get(url, [data], [callback])

Perform a HTTP GET request. This function is equivalent to:

http.request({
    url: url,
    data: data
}, callback);

Parameters

  • url required The URL to which the request is sent.
  • data optional The data to be sent to the server.
  • callback optional The function to execute when the request completes. This function gets passed two arguments:
    • data The data returned from the server.
    • error An array contains status code and error message describing the error, if one occurred.

http.post(url, [data], [callback])

Perform a HTTP POST request. This function is equivalent to:

http.request({
    url: url,
    type: 'POST',
    data: data
}, callback);

Parameters

  • url required The URL to which the request is sent.
  • data optional The data to be sent to the server.
  • callback optional The function to execute when the request completes. This function gets passed two arguments:
    • data The data returned from the server.
    • error An array contains status code and error message describing the error, if one occurred.