WLResourceRequest
This class encapsulates a resource request.
The resource can be an adapter that is deployed to an instance of MobileFirst Server, or a resource on an external resource server.
The class provides several send methods with different inputs for the body of a request.
A successful response is any response with a status in the 2xx range from the server.
A response with a 4xx or 5xx status from the server is considered a failure.
Static Member Summary
Static Public Members | ||
public static get |
|
|
public static get |
|
|
public static get |
|
|
public static get |
|
|
public static get |
|
|
public static get |
|
Constructor Summary
Public Constructor | ||
public |
constructor(url: string, method: string, timeout: number, scope: string, backendServiceName: string) Create a new ResourceRequest object to fetch data from a resource endpoint. |
Method Summary
Public Methods | ||
public |
Adds a header to this resource request. |
|
public |
async getAllHeaders(): Promise<Object> Get a list of all the headers added to this request |
|
public |
async getHeaders(headerName: string): Promise<Array> Get a list of values for a given header |
|
public |
Get the HTTP method of this resource request. |
|
public |
async getQueryParameters(): Promise<Object> Get all the query parameters that are added to this request. |
|
public |
async getQueryString(): Promise<string> Get the query string of this request. |
|
public |
async getTimeout(): number Get the timeout for this resource request. |
|
public |
Returns the URL for this resource request. |
|
public |
removeHeaders(headerName: string) Removes headers with the specified name for this resource request. |
|
public |
Initiate a request to the resource without any content in the body. |
|
public |
async sendWithFormParameters(formParams: Object): Promise<Map, String> Initiate a request to the resource with the specified form parameters in the body. |
|
public |
async sendWithJSON(jsonObject: Object): Promise<Map, String> Initiate a request to the resource with the specified JSON content in the body. |
|
public |
async sendWithRequestBody(body: string): Promise<Map, String> Initiate a request to the resource with raw text in the body. |
|
public |
setHeaders(headers: Object) Sets the specified headers for this resource request. |
|
public |
setQueryParameters(params: Object) Set the query parameters that should be added to this request. |
|
public |
setTimeout(timeoutInMilliseconds: number) Sets the expiration period (timeout) for this resource request. |
Static Public Members
Public Constructors
public constructor(url: string, method: string, timeout: number, scope: string, backendServiceName: string) source
Create a new ResourceRequest object to fetch data from a resource endpoint.
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 through Mobile Foundation API Connector .
To access a microservice URL such as http://mybluemix.net/resorts/cities, the Mobile Foundation runtime backend service is configured as follows
{"service": "tourpackage", "baseUrl":"http://mybluemix.net/resorts"}
Pass null or undefined as the scope and a desired timeout value. Any other value for the scope will be ignored. The default value of timeout is 30000 ( 30 seconds).
For example : var request = new WLResourceRequest("cities", WLResourceRequest.GET, 30000, undefined, "tourpackage");
Params:
Name | Type | Attribute | Description |
url | string | The URL of the resource - either as an absolute URL or a relative URL. |
|
method | string | The HTTP method to call the resource. Use WLResourceRequest.GET, WLResourceRequest.POST, WLResourceRequest.HEAD, WLResourceRequest.PUT, WLResourceRequest.DELETE, WLResourceRequest.OPTIONS, WLResourceRequest.TRACE |
|
timeout | number | Optional. The interval in milliseconds after which the call to the resource should time out. The default timeout is 30 seconds. |
|
scope | string | Optional. The scope protecting the particular resource. |
|
backendServiceName | string | Name of the backend service configured on server to fetch data from the backend service. For example : "tourpackage" |
Public Methods
public addHeader(name: string, value: string) source
Adds a header to this resource request. This method allows response headers to have multiple values.
public async getAllHeaders(): Promise<Object> source
Get a list of all the headers added to this request
public async getHeaders(headerName: string): Promise<Array> source
Get a list of values for a given header
Params:
Name | Type | Attribute | Description |
headerName | string | The name of the header |
public async getQueryParameters(): Promise<Object> source
Get all the query parameters that are added to this request.
public removeHeaders(headerName: string) source
Removes headers with the specified name for this resource request.
Params:
Name | Type | Attribute | Description |
headerName | string | The name of the header to remove. |
public async send(): Promise<Map, String> source
Initiate a request to the resource without any content in the body.
Return:
Promise<Map, String> | If success, a map with the response from the server. The map contain following key strings. "responseText" - Original response text from the server. "responseJSON" - in case the response is a JSON response, otherwise map will not have the field. "userInfo" - user information. "headers" - Retrieves the headers from the response. "status" - HTTP status from the response If failure, more information about the error. |
public async sendWithFormParameters(formParams: Object): Promise<Map, String> source
Initiate a request to the resource with the specified form parameters in the body.
Params:
Name | Type | Attribute | Description |
formParams | Object | The JSON object of form parameters in name-value pairs |
Return:
Promise<Map, String> | If success, a map with the response from the server. The map contain following key strings. "responseText" - Original response text from the server. "responseJSON" - in case the response is a JSON response, otherwise map will not have the field. "userInfo" - user information. "headers" - Retrieves the headers from the response. "status" - HTTP status from the response If failure, more information about the error. |
public async sendWithJSON(jsonObject: Object): Promise<Map, String> source
Initiate a request to the resource with the specified JSON content in the body.
Params:
Name | Type | Attribute | Description |
jsonObject | Object | The body of the request in JSON format. |
Return:
Promise<Map, String> | If success, a map with the response from the server. The map contain following key strings. "responseText" - Original response text from the server. "responseJSON" - in case the response is a JSON response, otherwise map will not have the field. "userInfo" - user information. "headers" - Retrieves the headers from the response. "status" - HTTP status from the response If failure, more information about the error. |
public async sendWithRequestBody(body: string): Promise<Map, String> source
Initiate a request to the resource with raw text in the body.
Params:
Name | Type | Attribute | Description |
body | string | The body that is part of the request. |
Return:
Promise<Map, String> | If success, a map with the response from the server.The map contain following key strings. "responseText" - Original response text from the server. "responseJSON" - in case the response is a JSON response, otherwise map will not have the field. "userInfo" - user information. "headers" - Retrieves the headers from the response. "status" - HTTP status from the response If failure, more information about the error. |
public setHeaders(headers: Object) source
Sets the specified headers for this resource request. For a header that is already set, the new value overwrites the previous value.
Params:
Name | Type | Attribute | Description |
headers | Object | A JSON object containing a map of all headers to be set for this request. |