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
- optionsrequiredA set of key/value pairs that configure the request.
- urlrequiredThe URL to which the request is sent.
- typeoptionalThe type of the request to make. Valid values are
GET
,POST
,PUT
,PATCH
andDELETE
. Default isGET
. - dataoptionalThe data to be sent to the server.
- headersoptionalAn object of additional header key/value pairs to send along with the request.
- usernameoptionalA username to be used with the request in response to an HTTP access authentication request.
- passwordoptionalA password to be used with the request in response to an HTTP access authentication request.
- callbackoptionalThe function to execute when the request completes. This function gets passed two arguments:
- dataThe data returned from the server.
- errorAn 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
- urlrequiredThe URL to which the request is sent.
- dataoptionalThe data to be sent to the server.
- callbackoptionalThe function to execute when the request completes. This function gets passed two arguments:
- dataThe data returned from the server.
- errorAn 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
- urlrequiredThe URL to which the request is sent.
- dataoptionalThe data to be sent to the server.
- callbackoptionalThe function to execute when the request completes. This function gets passed two arguments:
- dataThe data returned from the server.
- errorAn array contains status code and error message describing the error, if one occurred.