Getting data from a public REST service
In this tutorial, we try to call a public REST service from a task in Bonita. The public service is available on https://www.boredapi.com/api/activity. This service returns a response in JSON format. We will create a business process (using Bonita) with a simple task. On this task, we create a connector that calls this service and parse the returned response.
Create business process to access the REST service and to process its response
Using Bonita Studio, create a workflow as following.
Set the details of the activities as following.
Name |
Type |
Find something to do |
Service |
Get full response |
Human |
Get details |
Human |
Select the
Pool. In
Data tab, declare the following global process variables.
Name |
Data type |
response |
Text |
activity |
Text |
type |
Text |
participants |
Integer |
price |
Double |
link |
Text |
At the Find something to do activity, select Execution→Connectors in.
REST connector should already exist in your Bonita studio if you have already do
the previous tutorial. If not, click on
Access the bonita marketplace to install it.
Once the installation is complete, click on
Add. Select
REST from the left panel and
GET from the right panel, then
Next.
Name the connector as
RestService_Connector, then
Next.
-
Keep clicking
Next until reaching the
Output Operations step shown below.
Select the variable response from the first list to store the service response. Click on the pencil icon to edit the second list parameter.
Set the script
name to
getResponse then add the following java code to the script editor.
bodyAsObject.toString()
Click
OK.
Select the variable
activity from the first list. Click on the
pencil icon to edit the second list parameter. Set the script
name to
getActivity and add
bodyAsObject.activity to the script editor as following.
By the same way, add the following parameters.
First List |
Script name |
Java code to add in the script editor |
type |
getType |
bodyAsObject.type |
link |
getLink |
bodyAsObject.link |
price |
getPrice |
bodyAsObject.price |
participants |
getParticipants |
bodyAsObject.participants |
Click on
Finish.
At the Get full response activity, we will design a simple form to visualize the service response. To do this, click on the activity, then Execution → Form. In Target Form, create a new Form called FullResponseForm.
In the form page, drag and drop a
Text area. Set its
Label to
Response as following.
In the
FullResponseForm page, click on
create a new variable and fill the parameters as follows, then
Save.
Set the
value of the
Response text area to
response.value.
Save then close the form.
At the Get details activity, we will design a simple form to visualize the service response in detail. Click on the activity, then Execution → Form. In Target Form, create a new Form called DetailsForm.
In the form page, drag and drop 5
Text area labelled :
Activity,
Type,
Participants,
Price and
Link.
Select the
Activity text area. Set its
Value to
activity.value.
Select the
Activity text area then
create new variable. Fill in the parameters as follows, then
Save.
By the same way, change the
Value and
create new variable for each of the 4 other text areas. Follow the table below to set variable parameters.
Text Area |
Value |
variable Name |
variable Type |
variable API URL |
Type |
type.value |
type |
External API |
../API/bpm/caseVariable/{{task.caseId}}/type |
Participants |
participant.value |
participants |
External API |
../API/bpm/caseVariable/{{task.caseId}}/participants |
Price |
price.value |
price |
External API |
../API/bpm/caseVariable/{{task.caseId}}/price |
Link |
link.value |
link |
External API |
../API/bpm/caseVariable/{{task.caseId}}/link |
Save the form, then close it.
Run the process
Save the process, then Run.
Start the Pool.
Refresh the Task List. Now, you can see the service response in JSON format in the Response text area.
Take the
Get full response from your
Task list, then submit.
Now, you can see the service response in details.
The service invited me to “Organize my pantry”, a “busy work” with only “one participant”. What about you! Which activity did it propose to you?
Back to top