wilton.js  v202103141
 All Namespaces Functions
Functions
web_httpClient Namespace Reference

wilton/web/httpClient
HTTP client for web-browsers. More...

Functions

Object sendRequest (String url, Object options, Function|Undefined callback)
 Send XHR request to server.

Detailed Description

This module allows to use XHR requests in web-browsers with API similar to wilton/httpClient

Usage example:

// send GET request
http.sendRequest("https://google.com", function(err, resp) {
console.log(resp.data);
});
// send POST request and allow error responses
var resp2 = http.sendRequest(url, {
data: "foo",
meta: {
abortOnResponseError: false,
method: "POST" // optional, if data is specified
}
}, function(err, resp) { ... });

Function Documentation

Object web_httpClient::sendRequest ( String  url,
Object  options,
Function|Undefined  callback 
)

Sends XHR request to server, calls callback on response received or on timeout.

Parameters
urlString URL of the HTTP server
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive response or error, see response description below
Returns
Object XHR object

Options

  • data String|Object|Undefined data to send in request body, specified object will be converted to JSON
  • meta: Object|Undefined request metadata
    • headers Object|Undefined request headers in "Header-Name": "value" format
    • method String|Undefined HTTP method to use for this request, default value is GET, if request data is not specified, POST otherwise
    • abortOnResponseError Boolean|Undefined whether to throw an Error, if response status code >=400 is returned, default value: true
    • timeoutMillis Number|Undefined maximum time the request is allowed to take, in milliseconds, default value: 0

Response

  • data String data as a String
  • json Function parses the contents of data field as a JSON, resulting JSON object is cached for this response instance - the same object is returned for the following json() invocations
  • headers Object response headers in "Header-Name": "value" format
  • effectiveUrl String final URL (after possible redirection) that was used
  • responseCode Number HTTP response status code