Use Call REST Service Action in a Microflow
Introduction
In your Mendix app, you can use information from REST services. This how-to shows you how to do that through an example where you create an app that retrieves Wikipedia pages from a REST service. The resulting app is available for download.
This how-to teaches you how to do the following:
- Call the REST service in a microflow
Creating a JSON Structure
The Wikipedia REST service allows you to retrieve the summary of a page. This service can be reached at https://en.wikipedia.org/api/rest_v1/page/summary/{title}
, where {title}
is the title of a page.
The steps below use Studio Pro as an example of what the REST service returns:
-
Open your browser and go to https://en.wikipedia.org/api/rest_v1/page/summary/Tahiti. This calls the REST service with a
GET
request. The result is the summary of the Tahiti page on Wikipedia in JSON format. -
Copy the whole JSON snippet.
-
Create a module and name it RESTconsume.
-
Add the new JSON structure to your module by right-clicking the module in the App Explorer and selecting Add other > JSON structure.
-
Enter JSON_structure in the Name field and click OK.
-
In the JSON Structure dialog box, paste the JSON snippet in the General tab and click Refresh in the Structure section. This analyzes the structure of the JSON snippet and provides a representation of it.
-
Click OK
Creating an Import Mapping
An import mapping specifies how the JSON relates to entities. You can map the JSON to any entity you like. The following steps describe how to generate the entities and create an import mapping:
-
Right-click the module in the App Explorer and select Add other > Import Mapping.
-
Enter Import_mapping in the Name field and click OK.
-
In the Select schema elements for import mapping dialog box, click the radio button for JSON structure, then click Select.
-
Double-click JSON_structure in the Select JSON Structure dialog box.
-
Click Expand all, then click Check all.
-
Click OK. The Import_mapping document is displayed with the JSON structure on the right.
-
Click Map automatically in the editor toolbar.
-
A pop-up box is displayed that you that can See the details for the changes that have been applied. Click Details to see details of the mapping to the entities that are generated to match the JSON structure.
-
Click Close to see the entities corresponding to the JSON structure.
Adding an Input Entity to the Domain Model
The service takes the title of the page as an input and returns the summary of the page from Wikipedia.
In this section, you will create an entity that represents this input and associate it with its summary.
To add an input entity to the domain model, follow these steps:
-
In the App Explorer, double-click the Domain model.
-
Rename Root to Summary.
-
From the Toolbox, drag an entity into the domain model.
-
Double-click the entity and enter Input for the Name.
-
For Persistable, select No.
-
On the Attributes tab, click New to add a string attribute, name it Title, then click OK.
-
Drag an association from Input to Summary.
-
Double-click Import_mapping in the App Explorer and from the Connector pane, drag Input as the input parameter for the input mapping.
-
Double-click Summary.
-
In the Map entity ‘Summary’ from schema object element ‘Root dialog box, Set association to Yes and select the RESTconsume_input_Summary for the Association to enable the import mapping to set the association:
-
Click OK.
Calling the REST Service in a Microflow
You will now call the REST service in a microflow to build the integration with Wikipedia. The input parameter for the microflow is Input, which is associated with Summary.
To call the REST service in a microflow, follow these steps:
-
Right-click the module and select Add Microflow. Then, accept the default name Microflow and click OK.
-
From the toolbar, drag a Parameter object to the microflow document.
-
For the Data type, click Select and select the Input entity as the input parameter. Click OK.
-
Right-click and select Add > Activity and insert this to the microflow. Double-click the activity and select Call REST service to change the type of action.
-
In the Call REST dialog box, click Edit for the Location and add the following to Template:
https://en.wikipedia.org/api/rest_v1/page/summary/{1}
, with the parameter being the Title attribute from the Input parameter$Input/Title
. Click OK. -
In the Response tab, the response has to be mapped using the import mapping. For Response handling, select Apply import mapping.
-
For Mapping, click Select and double-click Import_mapping. For the Parameter, select Input.
-
For Output select Yes for Store in variable and specify Summary for the Variable name.
-
Click OK.
-
Right-click after the Call REST service object and select Insert > Activity. Double-click it and change it to a Change object.
-
For the Input Object, select Input (RESTconsume.Input).
-
For Refresh in client, select Yes. This ensures the summary is displayed on the screen.
-
To add an action, click New.
-
On the Edit Change Item dialog box, for Member, select RESTconsume.Input_Summary (RESTconsume.Summary).
-
Under Value, enter
$Summary
. -
Click OK.
-
Click OK. You have created microflow that takes the title of an article as input and associates it with its summary.
You have successfully consumed a REST service and created a microflow to show the results. The rest of this how-to describes how to use this microflow in an app so you can see the REST call in action.
Creating a Page
To create a page for this app, follow these steps:
-
Open the Home_Web and add a Data view.
-
Right-click the [Unknown] bar, click Select entity and select the Input entity.
-
For the Data source – Type, select Microflow.
-
For the Microflow Name, enter CreateInput. Note that when this page loads, it needs a new Input object. You will define the CreateInput microflow that creates this object in Filling In the Create Input Microflow.
-
Click OK.
-
In the App Explorer under Structure, add a new Table with one row and two columns.
-
Drag the Title field onto the left column.
-
Delete the Summary field.
-
From the App Explorer, drag a Call Microflow button into the right column.
-
In the Select Microflow dialog box, expand RESTconsume and select Microflow. This is the microflow you created in the Calling the REST Service in a Microflow section above.
-
Double-click the Microflow button and in the Edit Action Button dialog box, enter Get summary for button caption.
-
Add a Data view below the table (inside the other data view).
-
From the Connector, drag the Summary entity onto the [Unknown] bar and click OK.
-
From this data view, delete all the fields except Extract.
-
Double-click Extract.
-
For Show label, select No, then click OK.
-
Delete the Save and Cancel buttons.
Filling in the Create Input Microflow
Now, all that is left is to have the CreateInput microflow create a new Input object.
To fill in the CreateInput microflow, follow these steps:
- In the App Explorer, double-click CreateInput.
- From the Toolbox, drag a Create object activity to the microflow.
- Double-click the activity.
- For Entity, click Select and double-click Input, then click OK.
- Double-click the red End event.
- Under Return value, enter
$NewInput
, then click OK.
You can now start your app and get summaries from Wikipedia.
Read More
- Consume a Complex Web Service
- Consume a Simple Web Service
- Export XML Documents
- Import XML Documents
- Import Excel Documents
- Expose a Web Service
- Expose Data to BI Tools Using OData
- Security Considerations