XML Data Transfer and Usage Between Tasks
Scenario: A call will be made to an API that returns XML data, and each element in the array within the returned result will be sent to another API with a specific format.
In this case, two API call connectors should be added as tasks. The first one is necessary for reading, and the second one is needed within a loop for writing.
Task-1: Reading data with API call connector
- Clicking on the API Call option from the dialog where tasks are listed.
- Selecting the "Once" option to specify that the task will run only once.
- Entering the relevant API access information for data reading.
- Setting the "xmlResponse" value for the output key information to be able to use the data returned from the API in the next task. (You can name this value as you prefer.)
When this task is executed, the XML data below will be returned in this example, meaning the "xmlResponse" value will contain the following data:
<root> <users> <user> <id>1</id> <name>user 1</name> <description>Description of user 1</description> </user> <user> <id>2</id> <name>user 2</name> <description>Description of user 2</description> </user> <user> <id>3</id> <name>user 3</name> <description>Description of user 3</description> </user> </users> </root> |
Task-2: Sending data to the API with the API Call connector
- Clicking on the API Call option from the dialog where tasks are listed.
- Selecting the "Loop" option to indicate that the task will iterate over the array data received from the previous task.
- Writing the XPath specifying where the value of the entered output key information ("xmlResponse") from the previous task should be processed as an array.
- When writing XPath, to specify processing the previous data as XML, the "#/" sign is added to the key information ("xmlResponse"). Then, the path where the array elements are located is entered as XPath. In this case, for this example, the following should be written where XPath is specified: {{xmlResponse#/root/users/user}}
- Entering the relevant API access information where the data will be sent.
- Entering the desired template message information to be sent. If the values of the elements in the loop are to be used within the template message, it is ensured that the data within the respective element is added to the template using the "LOOP_VARIABLE" expression. Along with LOOP_VARIABLE, the XPath information pointing to the XML data within the element designated for the loop should be entered.
In this example, when the expression {{xmlResponse#/root/users/user}} is executed on the "xmlResponse" data returned from the previous task, it returns 3 elements, indicating that this task will run 3 times.
In the first iteration, the XML within the LOOP_VARIABLE value will be as follows:
<user> <id>1</id> <name>user 1</name> <description>Description of user 1</description> </user> |
Let's assume that the desired template to be applied to this XML is as follows:
{ "test_id":"{{LOOP_VARIABLE#/id/text()}}", "test_name":"{{LOOP_VARIABLE#/name/text()}}", "test_description":"{{LOOP_VARIABLE#/description/text()}}" } |
Note that the text() method is used at the end of the XPath alongside LOOP_VARIABLE to obtain the target data here.
In this case, the request body to be sent with the API Call will be as follows:
{ "test_id": "1", "test_name": "user 1", "test_description": "Description of user 1" } |