Class WLResourceRequest

WLResourceRequest

JavaScript File:
wlresourcerequest.js
Description:
The WLResourceRequest object is used to send a request to any protected or unprotected resource using an absolute or relative URL. It exposes a set of methods that allow you to set up and configure the requested object.

WLResourceRequest object automatically handles the MobileFirst OAuth-based security model protocol and invokes the required challenges.
The WLAuthorizationManager and WLResourceRequest classes are supported for the following hybrid environments only: Android, iOS, Windows Phone 8 and Window 8.

Example:
var request = new WLResourceRequest('/adapters/sampleAdapter/multiplyNumbers', WLResourceRequest.GET);
request.setQueryParameter('params', [5, 6]);
request.send().then(
     function(response) {
         // success flow, the result can be found in response.responseJSON
     },
     function(error) {
         // failure flow
         // the error code and description can be found in error.errorCode and error.errorMsg fields respectively
     }
);

Constructor Summary
Constructor Attributes Constructor Name and Description
 
WLResourceRequest(url, method, options)
Method Summary
Method Attributes Method Name and Description
 
addHeader(name, value)
Adds a new header.
 
getHeader(name)
Returns a first header value stored with the specified header name.
 
Returns array of header names.
 
getHeaders(name)
Returns array of header values.
 
Returns current request method.
 
Returns query parameters as a JSON object with key-value pairs.
 
Returns request timeout, in milliseconds.
 
getUrl()
Returns request URL.
 
send(content)
Sends the request to a server.
 
Sends the request to a server with URL encoded form parameters.
 
setHeader(name, value)
Sets a new header or replaces an existing header with the same name.
 
setHeaders(requestHeaders)
Sets request headers.
 
setQueryParameter(name, value)
Sets a new query parameter.
 
setQueryParameters(parameters)
Sets query parameters.
 
setTimeout(requestTimeout)
Sets request timeout.
Constructor Detail
WLResourceRequest
WLResourceRequest(url, method, options)
Parameters:
{String} url - Mandatory. Specifies absolute or relative URL. If you send a request to an adapter, you must add the /adapters path element. For example: /adapters/myAdapterName/myMethodName
{String} method - Mandatory. Request method, usually WLResourceRequest.GET or WLResourceRequest.POST
{object} options - Optional. An object containing the following properties:
  • options.timeout: (integer) Optional. Request timeout, in milliseconds.
  • options.scope: (string) Optional. Protecting scope of the requested resource.
Method Detail
addHeader
addHeader(name, value)
Adds a new header. If a header with the same name already exists, the header value will be added to the existing header. The name is case insensitive.
Parameters:
{String} name - Mandatory. Header name.
value - Mandatory. Header value. The value must be of a simple type (string, number or boolean).

getHeader
getHeader(name)
Returns a first header value stored with the specified header name. The value is returned as a string. Can be undefined if a header with the specified name does not exist.
Parameters:
{String} name - Mandatory. Header name.

getHeaderNames
getHeaderNames()
Returns array of header names. It can be empty, if no headers have been added.

getHeaders
getHeaders(name)
Returns array of header values.
Parameters:
{String} name - Optional. Header name. If header name is specified, this function returns an array of header values stored with this header, or undefined, if the specified header name is not found. If name is null, or undefined, this function returns all headers.

getMethod
getMethod()
Returns current request method. A valid request method must have been passed to constructor.

getQueryParameters
getQueryParameters()
Returns query parameters as a JSON object with key-value pairs.

getTimeout
getTimeout()
Returns request timeout, in milliseconds.

getUrl
getUrl()
Returns request URL. The URL must have been passed to the constructor.

send
send(content)
Sends the request to a server.
Parameters:
content - Optional. Body content. It can be of a simple type (like string), object, or undefined.
Returns:
Promise
Example:
var request = WLResourceRequest(url, method, options);
request.send(content).then(
  function(response) {
    // success flow
  },
	 function(error) {
    // fail flow
  }
);

sendFormParameters
sendFormParameters(json)
Sends the request to a server with URL encoded form parameters.
Parameters:
{Object} json - Mandatory. Body content as JSON object or string as form data. The JSON object values must be a simple type. The content type will be set to application/x-www-form-urlencoded.
Returns:
Promise
Example:
var request = WLResourceRequest(url, method, options);
request.send(json).then(
  function(response) {
    // success flow
  },
  function(error) {
    // fail flow
  }
);

setHeader
setHeader(name, value)
Sets a new header or replaces an existing header with the same name.
Parameters:
{String} name - Mandatory. Header name.
value - Mandatory. Header value. The value must be of a simple type (string, boolean or value).

setHeaders
setHeaders(requestHeaders)
Sets request headers. The existing headers are replaced.
Parameters:
{Object} requestHeaders - Optional. JSON object with request headers. Each header value should be either string, or array of strings. This function throws an error if one of the specified header values is not valid. If this parameter is not specified, all headers will be removed.

setQueryParameter
setQueryParameter(name, value)
Sets a new query parameter. If a parameter with the same name already exists, it will be replaced.
Parameters:
{String} name - Mandatory. Parameter name.
value - Mandatory. Parameter value. Should be string, number or boolean.

setQueryParameters
setQueryParameters(parameters)
Sets query parameters.
Parameters:
{object} parameters - Optional. A JSON object with key-value pairs. If this parameter is null, or undefined, the query parameters are cleared.

setTimeout
setTimeout(requestTimeout)
Sets request timeout. If timeout is not specified, then a default timeout will be used.
Parameters:
{Integer} requestTimeout - Mandatory. Request timeout, in milliseconds.

© Copyright IBM Corp. 2011, 2016.