How payload, variable and attributes behave when passing through different flows in Anypoint Studio (Mule 4)
- November 26, 2020
When we pass through parent flow to child flow and come back to calling flow (parent flow), understanding how payload, variables and attributes (headers and query parameters) behave is important to design and build scalable architectures using MuleSoft.
Case 1: When using flow reference
When passing from parent flow to child flow using flow reference, child flow can be a flow, a sub-flow or a private flow. For all these flows, as a child flows, payload, variables and attributes behave in the same way.
Let’s assume in parent flow we have some attributes, payload and variables set. When we pass to the child flow using flow reference, all can be accessed in the child flow. Payload and variable can be modified in child flow and when returned to the parent flow, the changes are reflected in parent flow as well.
Let’s see the same with a working example.
Below is parent flow and a child flow, which is called using a flow reference.
When a request is made to HTTP listener, before going in the child flow, the following is set:
Query parameters = Mule
Payload = initial payload
Variable = initial_var_value
We can see the same in the debugger as well. A breakpoint is added at childFlow Flow Reference to view the same.
When we move to child flow, we can see we can access all this:
We can also modify payload and variable in child flow, we will be setting it to:
Payload = “intermediate_payload”
Variable = “intermediate_var_value”
When we step back in parent flow, the changes are reflected in parent flow as well.
Attributes remained unchanged.
We can access all this and modify it in parent flow as well. Finally, we get final payload, and final variable as well.
Case 2: When using HTTP Request
When passing from parent flow to child flow using HTTP Request, attributes are updated. (Attributes at starting in parents flow are not equal to attributes. Payload goes as it is and can be modified in child flow as well. Variable cannot be passed as a variable — you can pass it using attributes for the request but not as a variable — because HTTP request has only attributes and payload in its body. As the variable isn’t passed, it cannot be accessed or modified from child flow. When coming back to calling flow (parent flow), modified payload and variable (which was not passed to child flow) can be accessed and modified in the parent flow. The same is shown in the exhibit below.
Let’s see the same with a working example.
Below is parent flow and a child flow, which is called using a HTTP Request.
When a request is made to HTTP listener, before going in the child flow the following is set:
Query parameters = Mule
Payload = initial payload
Variable = initial_var_value
We can see the same in debugger as well, a breakpoint is added at Request component to view the same.
As we move to child flow, the attributes are replaced with new request’s attributes. Payload remains the same, and variable isn’t passed to child flow so it cannot be accessed or modified from child flow.
Query parameter changed from “Mule” to “Anypoint”, and variable from parent flow isn’t accessible in child flow. However, we can set a new variable in child flow whose scope will be limited to child flow only. When we go back to parent flow, the changes made in payload are reflected. Attributes are lost again, and the variable that was defined in child flow is also lost. At the same time, the variable that’s defined in parent flow is now accessible and can be modified.
After modifying this, we get final payload and variable as follows at the end of the flow.
One thing to note here — if the method in HTTP Request is GET, the payload will be lost while moving from parent flow to child flow as GET HTTP request doesn’t have a body.
This is how payload, variables and attributes are passed and modified across the flows.
— By Sanket Kangle