WLResourceRequest
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 environments only:
Android, iOS, Windows 8.1, Window 10 UWP and Web.
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
}
);
The WLResourceRequest API can be used to allow mobile apps access to microservices hosted outside of Mobile Foundatione.
Mobile Foundation facilitates secure calls to microservice or enterprise backend service without involving adapters though Mobile Foundation API Connector.
To access a microservice URL such as "http://mybluemix.net/resorts/cities" and the Mobile Foundation runtime backend service is configured as follows
{"service": "resorts",
"baseUrl":"http://mybluemix.net/resorts"}
var options = { timeout : 30000, backendServiceName : "resorts"}
var request = new WLResourceRequest('/cities', WLResourceRequest.GET,options);
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 Attributes | Constructor Name and Description |
---|---|
WLResourceRequest(url, method, options)
|
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.
|
|
sendFormParameters(json)
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.
|
/adapters
path element.
For example: /adapters/myAdapterName/myMethodName
WLResourceRequest.GET
or WLResourceRequest.POST
options.timeout
: (integer) Optional. Request timeout, in milliseconds.options.scope
: (string) Optional. Protecting scope of the requested resource.options.backendServiceName: (string) Optional. Name of the backend service configured on server to fetch data from. For example :resorts
name
is null, or undefined,
this function returns all headers.
var request = WLResourceRequest(url, method, options);
request.send(content).then(
function(response) {
// success flow
},
function(error) {
// fail flow
}
);
application/x-www-form-urlencoded.
var request = WLResourceRequest(url, method, options);
request.send(json).then(
function(response) {
// success flow
},
function(error) {
// fail flow
}
);