Resource request from Flutter applications
Overview
Persistent Mobile Foundation applications can access resources using the MFResourceRequest REST API.
The REST API works with all adapters and external resources.
Prerequisites:
- Ensure you have added the PMF SDK to your Flutter application.
- Learn how to create adapters.
WLResourceRequest
The MFResourceRequest class handles resource requests to adapters or external resources.
Create a MFResourceRequest object and specify the path to the resource and the HTTP method.
Available methods are: GET, POST, PUT and DELETE.
URI adapterPath = new URI("/adapters/JavaAdapter/users",UriKind.Relative);
MFResourceRequest request = MFResourceRequest(
adapterPath,
MFResourceRequest.GET,
);
- For JavaScript adapters, use
/adapters/{AdapterName}/{procedureName} - For Java adapters, use
/adapters/{AdapterName}/{path}. Thepathdepends on how you defined your@Pathannotations in your Java code. This would also include any@PathParamyou used. - To access resources outside of the project, use the full URL as per the requirements of the external server.
- Optional
- timeout: Request timeout in milliseconds
- scope: 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 .send() method.
MFResourceRequest response = await request.send();
Use the MFResourceRequest response object to get the data that is retrieved from the adapter.
The response object contains the response data and you can use its methods and properties to retrieve the required information. Commonly used properties are ResponseText, ResponseJSON (if the response is in JSON) , Success (if the invoke was successful or failure) and HTTPStatus (the HTTP status of the response).
Parameters
Before sending your request, you may want to add parameters as needed.
Path parameters
As explained above, path parameters (/path/value1/value2) are set during the creation of the MFResourceRequest object:
Uri adapterPath = Uri.parse("/adapters/JavaAdapter/users/value1/value2");
MFResourceRequest request = MFResourceRequest(
adapterPath,
MFResourceRequest.GET,
);
Query parameters
To send query parameters (/path?param1=value1...) use the SetQueryParameter method for each parameter:
request.setQueryParameters ("param1","value1");
request.setQueryParameters ("param2","value2");
JavaScript adapters
JavaScript adapters use ordered nameless parameters. To pass parameters to a Javascript adapter, set an array of parameters with the name params:
request.SetQueryParameter("params","['value1', 'value2']");
This should be used with GET.
Form parameters
To send form parameters in the body, use .Send(Dictionary<string, string> formParameters) instead of .Send():
Map<String, String> formParams = {};
formParams["height"] = heightController.text;
await request.send(formParams);
JavaScript adapters
JavaScript adapters use ordered nameless parameters. To pass parameters to a Javascript adapter, set an array of parameters with the name params:
formParams.Add("params","['value1', 'value2']");
This should be used with POST.
Header parameters
To send a parameter as an HTTP header use .SetHeader() API:
Map<String, String> headerCollection = {};
headerCollection["key"] = "value";
await request.setHeaders(headers: 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.
The response
The MFResponse object contains the response data and you can use its methods and properties to retrieve the required information. Commonly used properties are ResponseText (String), 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 an error property.
For more information
For more information about MFResourceRequest, refer to the user documentation.
Sample application
The PmfExample 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.
Click to download the Xamarin project.
Click to download the adapter Maven project.
Sample usage
Follow the sample’s README.md file for instructions.
▲