Mule 4自定义策略动态添加请求头

Mule 4自定义策略动态添加请求头,mule,mule-esb,Mule,Mule Esb,我是Mulesoft的新手,是一名入门级程序员。我一直在试图找出如何实施Mule 4自定义策略。这是在线文档: 根据这些示例,我成功地添加了请求和响应头。我的主要目标是在使用变量时添加请求头。我正在尝试MEL()调用变量名,但它不起作用。但是,每当我尝试记录变量时,它都会返回正确的值 <?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesoft.org/schema/mule/core"

我是Mulesoft的新手,是一名入门级程序员。我一直在试图找出如何实施Mule 4自定义策略。这是在线文档:

根据这些示例,我成功地添加了请求和响应头。我的主要目标是在使用变量时添加请求头。我正在尝试MEL()调用变量名,但它不起作用。但是,每当我尝试记录变量时,它都会返回正确的值

<?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>

#[{'TEST_HEADER':'TEST'}]
#[{'Header_Added':'#[vars.Header]}]
#这一步不像我希望的那样有效
#日志记录打印标题的值
我正在尝试添加
#[vars.header]
作为请求头名称,可能还有值?我是否需要创建另一个变量以获得标题值?有人能告诉我正确的方向吗


谢谢

Mule 4使用dataweave表达式。尝试删除引号,使其不被视为静态字符串,并删除嵌套的表达式括号“#[]”:

#[{'Header_添加]:vars.Header}]
或者另一个技巧,如果需要访问字符串中间的变量,可以使用$()语法:

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