【问题标题】:Mule 4 Custom Policy Adding Request Headers DynamicallyMule 4 自定义策略动态添加请求标头
【发布时间】:2019-02-12 14:22:56
【问题描述】:

我是 Mulesoft 的新手,也是一名入门级程序员。我一直在试图弄清楚如何实施 Mule 4 自定义策略。这是在线文档:https://docs.mulesoft.com/api-manager/2.x/http-policy-transform#add-headers

按照示例,我成功地添加了请求和响应标头。我的主要目标是在使用变量时添加请求标头。我正在尝试 MEL (https://docs.mulesoft.com/mule-runtime/3.7/mule-expression-language-examples) 尝试调用变量名,但它不起作用。但是,每当我尝试记录变量时,它都会返回正确的值。

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

我正在尝试添加 #[vars.header] 作为请求标头名称,可能还有值?我需要创建另一个变量来获得标题值吗?有人可以指导我正确的方向吗?

谢谢

【问题讨论】:

    标签: mule mule-esb


    【解决方案1】:

    Mule 4 使用数据编织表达式。尝试删除引号,使其不被视为静态字符串并删除嵌套表达式括号'#[]':

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

    或者另一个提示,如果您需要访问字符串中间的 var,您可以使用 $() 语法:

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

    【讨论】:

      猜你喜欢
      • 2013-11-13
      • 1970-01-01
      • 2016-11-14
      • 2012-11-15
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      • 2020-03-16
      相关资源
      最近更新 更多