Outsystems - Retrieving Data From A REST API (GET)
Outsystems - Retrieving Data From A REST API (GET)
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 1
Table of Contents
Table of Contents 2
Introduction 3
Connect to an Environment 3
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 2
Introduction
In this lab, we are going to integrate with a REST Web Service to retrieve data from an external
system. We are going to use the GET method to read information of the Contacts in the
external system and then display that data in our application.
Connect to an Environment
When we open Service Studio for the first time, we will need to connect to an environment
where the OutSystems platform server generates, optimizes, compiles, and deploys
OutSystems applications.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 3
a) If you are using your Personal Environment, you can find its address in the
OutSystems website and log in.
c) Back in Service Studio, use that Environment and login with your OutSystems
community email (username) and password.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 4
1) In the Applications, open the Environment menu and select Open Files…
2) In the Open dialog, change the File Type dropdown option to OutSystems Application
Pack (*.oap) and then open the REST Contacts - GET.oap.
The quick start application is simply to speed up the setup part and start right away working
with the external REST web service.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 5
The GET request is known as a an idempotent and safe method as there are no changes in the
resource when calling it and the results are always the same for identical requests.
Before you consume any REST API it’s important to gather all the information you need from
the REST API documentation. Information such as the expected structures and some examples
maybe be really useful when consuming an external service. In this lab, the documentation for
the external REST Web Service is available here.
In this section we will create the integration with the external REST Web Service. The data that is
retrieved from this Web Service will later on be used and displayed on a Web Screen.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 6
a) Switch to the Logic tab and in the Integrations folder, right-click the REST
integration element and select C
onsume REST API.... This opens a window to
configure the REST method that you want to consume.
b) On the new window, click the A
dd Single Method button.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 7
NOTE: The Add All Methods option allows to integrate with REST Web Services
that provide a Swagger definition file. This speeds up the integration process a
lot, however not all Web Services provide this. Regardless if Swagger is available
or not, you can use the Single Method option.
https://foundation.outsystems.net/ContactsAPI/rest/v1/contacts/
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 8
NOTE: Service Studio allows you to Test REST API methods. This feature will also
speed up the integration since the data type of the response can be automatically
inferred. In situations where Test is not available or not possible you can set a
sample Body response in the Body tab. Usually documentation provide some
sample responses.
For this particular case, the sample response found in the documentation is like
this:
{
"ErrorMessage": "string",
"Contacts": [
{
"Title": "string",
"Name": "string",
"Gender": "string",
"Phone": "string",
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 9
"Birthday": "2014-12-31",
"Occupation": "string",
"City": "string",
"State": "string",
"Country": "string",
"Email": "string",
"Company": "string"
}
],
"Success": true
}
f) After clicking the Test button, the Response tab at the bottom should be
populated with the data retrieved from invoking the REST API method.
g) Click the Copy to Response Body button. The Body tab should now have the
sample response obtained in the previous step.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 10
h) Click the OK button to close the Consume REST API Method.
NOTE: This step is not mandatory, but it allows developers to have custom or
more familiar names in their integrations. You may also change the names of the
methods which can be useful when the auto-generated names are more
unfamiliar.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 11
j) Switch to the Data tab, and locate the ContactItem Structure under the
ContactsAPI.
The C
ontactItem and GetContactsResponse structures were automatically created
by Service Studio when the API Method was added in previous steps.
Expanding the C
ontactItem structure allows you to see all attributes of Contact
(Title, Name, Gender, …). These attributes were also automatically inferred by
Service Studio from the sample response of the API method.
NOTE: Just like renaming the API method, this step is also not mandatory.
However, renaming it makes development easier since names become more
familiar. It also allows for Service Studio to automatically set variables data types
based on their names. For Instance, when creating a variable named C
ontact,
Service Studio will automatically set its type to the Contact structure.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 12
In our sample app, the Contacts screen has already been created. Let’s now implement the
Preparation that will retrieve the list of Contacts.
1) In the Preparation of the Contacts screen, invoke the ContactsAPI to retrieve the list of
contacts from the External REST Web Service.
b) Switch to the Logic tab and locate the GetContacts REST method that we have
just defined. Drag and drop the method on the Preparation.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 13
2) Validate the result of the REST API method and show the error message on fail.
a) Drag an If a
nd drop it after the GetContacts, then set its Condition t o
GetContacts.Response.Success
b) Drag an End a
nd drop it to the right of the If, then create the True branch
connector from the If to the new End.
c) Drag a Run Server Action and drop it on the False branch connector of the If.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 14
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 15
3) Now that we have a way to retrieve a list of records we need to create the interface to
display them to the user. For that we are going to show them in a Table Records widget.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 16
GetContacts.Response.Contacts
d) Let’s switch to the Data tab and under the Structures folder locate the Contact
structure.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 17
f) Repeat the previous step for the Gender, Occupation a
nd Company fields.
g) You should end up having the Contacts screen with a Table Records with 4
columns.
4) Publish the application using 1-Click Publish button and verify that the publish
completed successfully in the 1-Click Publish Tab.
a) Click on the 1
-Click Publish button to publish the module to the server.
b) Verify in the 1-Click Publish tab that the publishing process was successful.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 18
NOTE: The contacts shown may vary depending on the data provided by the
external system.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 19
End Lab
In this lab, we integrated with a REST Web Service, namely a GET method to retrieve the list of
Contacts provided by the external system.
To accomplish that, we have used the OutSystems visual interface to consume the REST
method, that automatically infers the output structure of the method.
Once we consumed the API, we implemented the logic to display the information in the
Contacts screen, firstly adding the method on the Preparation and then creating the interface
to show some of the Contacts’ data on the screen.
At the end of this exercise you should be able to integrate with a simple REST API to retrieve
data and display it in your application.
OutSystems Inc. 5
901 Peachtree Dunwoody Road, N.E. Building C, Suite 495, Atlanta, GA 30328 20