How payload, variable, and attributes behave when passing through different flows in Anypoint Studio(Mule 4)

Sanket Kangle
5 min readMay 30, 2021

When we pass through parent flow to child flow and come back to calling flow(parent flow), Understanding how a payload, variables, and attributes(headers, query parameters, etc) 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. The same is shown in the following exhibit.

There is one target field of the Flow Reference, when this field is set, the payload that is changed in the child flow will be updated in the variable specified by the target, and the message.payload will remain the same as it was before calling the flow reference.

Let us 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 in parent flow as well, finally, we get the 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 a variable is not passed, it cannot be accessed or modified from child flow. When coming back to calling flow(parent flow), new attributes, 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 us see the same with a working example.

Below is parent flow and a child flow which is called using an 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 the debugger as well, a breakpoint is added at the 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 is not passed to child flow so it cannot be accessed nor modified from child flow.

Query parameter changed from “Mule” to “Anypoint”, and variable from parent flow is not 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 the payload are reflected, attributes are lost again and the variable that was defined in child flow is also lost. But at the same time, the variable that was defined in parent flow is now accessible and can be modified.

After modifying this we get the final payload and variable as follows at the end of the flow

One thing to note here is that, if the method in HTTP Request is GET, then the payload will be lost while moving from parent flow to child flow as GET HTTP request does not have a body.

This is how payload, variables, and attributes are passed, modified across the flows.

Give a clap if you found this article useful, suggestions are welcome in the comment.

If you want to connect with me, find me here:

--

--