Resource request from Xamarin applications
IBM Mobile Foundation applications can access resources using the
WorklightResourceRequest REST API.
The REST API works with all adapters and external resources.
- Ensure you have added the Mobile Foundation SDK to your Native Xamarin application.
- Learn how to create adapters.
WorklightResourceRequest class handles resource requests to adapters or external resources.
WorklightResourceRequest object and specify the path to the resource and the HTTP method.
Available methods are:
URI adapterPath = new URI("/adapters/JavaAdapter/users",UriKind.Relative); WorklightResourceRequest request = WorklightClient.CreateInstance.ResourceRequest(adapterPath,"GET");
- For Java adapters, use
pathdepends on how you defined your
@Pathannotations in your Java code. This would also include any
- To access resources outside of the project, use the full URL as per the requirements of the external server.
- timeout: Optional, request timeout in milliseconds
- scope: Optional, if you know which scope is protecting the resource - specifying this scope could make the request more efficient.
Sending the request
Request the resource by using the
WorklightResponse response = await request.send();
WorklightResponse response object to get the data that is retrieved from the adapter.
response object contains the response data and you can use its methods and properties to retrieve the required information. Commonly used properties are
ResponseJSON (if the response is in JSON) ,
Success (if the invoke was successful or failure) and
HTTPStatus (the HTTP status of the response).
Before sending your request, you may want to add parameters as needed.
As explained above, path parameters (
/path/value1/value2) are set during the creation of the
Uri adapterPath = new Uri("/adapters/JavaAdapter/users/value1/value2",UriKind.Relative); WorklightResourceRequest request = WorklightClient.CreateInstance.ResourceRequest(adapterPath,"GET");
To send query parameters (
/path?param1=value1...) use the
SetQueryParameter method for each parameter:
This should be used with
To send form parameters in the body, use
.Send(Dictionary<string, string> formParameters) instead of
Dictionary<string,string> formParams = new Dictionary<string,string>(); formParams.Add("height", height.getText().toString()); request.Send(formParams);
This should be used with
To send a parameter as an HTTP header use
System.Net.WebHeaderCollection headerCollection = new WebHeaderCollection(); headerCollection["key"] = value; request.AddHeader(headerCollection);
Other custom body parameters
.Send(requestBody)allows you to set an arbitrary String in the body.
.Send(JObject json)allows you to set an arbitrary dictionary in the body.
.Send(byte data)allows you to set an arbitrary byte array in the body.
WorklightResponse object contains the response data and you can use its methods and properties to retrieve the required information. Commonly used properties are
ResponseJSON (JSONObject) (if the response is in JSON) and
success (boolean) (success status of the response).
In case of request failure, the response object also contains a
For more information
For more information about WLResourceRequest, refer to the user documentation.
The ResourceRequestXamarin project contain a native Android and iOS application that makes a resource request using a Java adapter.
The adapter Maven project contains the Java adapter used during the resource request call.
Follow the sample’s README.md file for instructions.▲
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.