0
votes

I am very new to Mulesoft and an entry-level programmer. I have been trying to figure out how to implement Mule 4 custom policies. This is the online documentation: https://docs.mulesoft.com/api-manager/2.x/http-policy-transform#add-headers

Following the examples, I was successfully able to add request and response headers. My main goal is to add the request headers while using variables. I am trying MEL (https://docs.mulesoft.com/mule-runtime/3.7/mule-expression-language-examples) to try to call the variable names but it doesn't work. HOWEVER, whenever I try logging the variables it returns the correct value.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:http-policy="http://www.mulesoft.org/schema/mule/http-policy"
      xmlns:http-transform="http://www.mulesoft.org/schema/mule/http-policy-transform"
      xmlns:secure-properties="http://www.mulesoft.org/schema/mule/secure-properties"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
               http://www.mulesoft.org/schema/mule/http-policy http://www.mulesoft.org/schema/mule/http-policy/current/mule-http-policy.xsd
               http://www.mulesoft.org/schema/mule/http-policy-transform http://www.mulesoft.org/schema/mule/http-policy-transform/current/mule-http-policy-transform.xsd
               http://www.mulesoft.org/schema/mule/secure-properties http://www.mulesoft.org/schema/mule/secure-properties/current/mule-secure-properties.xsd">

    <http-policy:proxy name="{{{policyId}}}-custom-policy">
        <http-policy:source propagateMessageTransformations="true">
            <try>
                <set-variable variableName="header" value="TEST_HEADER"/>
                <logger level="INFO" message="#[vars.header]" />
                <http-transform:add-headers outputType="request">
                    <http-transform:headers>#[{'TEST_HEADER': 'TEST'}]</http-transform:headers>
                </http-transform:add-headers>
                <http-policy:execute-next/>
                <http-transform:add-headers outputType="response">
                    <http-transform:headers>#[{'Header_Added': '#[vars.header]'}]</http-transform:headers>
                    #this step doesn't work as I hoped it would
                </http-transform:add-headers>
                <logger level="INFO" message="#[vars.header]" />
                #logging prints the value of header
             </try>
        </http-policy:source>
    </http-policy:proxy>
</mule>

I am trying to add #[vars.header] as a request header name and possibly the value? Would I need to create another variable to have the header value? Can someone please guide me the right direction?

Thank you

1

1 Answers

1
votes

Mule 4 uses dataweave expressions. Try removing the quotes so its not treated as a static string and remove the nested expression brackets '#[]':

<http-transform:headers>#[{'Header_Added': vars.header}]</http-transform:headers>

Or another tip, if you need to access a var in middle of a string you can use $() syntax:

<http-transform:headers>#[{'Header_Added': '$(vars.header)'}]</http-transform:headers>