JavaScript Documentation / http
This object contains methods for performing HTTP requests.
- http.request(options, [callback])
- http.get(url, [data], [callback])
- http.post(url, [data], [callback])
http.request(options, [callback])
Performs a HTTP request.
Parameters
-
options
required
A set of key/value pairs that configure the request.
- url required The URL to which the request is sent.
-
type
optional
The type of the request to make. Valid values are
GET
,POST
,PUT
,PATCH
andDELETE
. Default isGET
. - data optional The data to be sent to the server.
- headers optional An object of additional header key/value pairs to send along with the request.
- username optional A username to be used with the request in response to an HTTP access authentication request.
- password optional 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' } }, data => { ui.alert(data.args.foo); });
http.get(url, [data], [callback])
Performs 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])
Performs 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.