我对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]
作为请求标头名称以及可能的值?我是否需要创建另一个变量以具有标头值?有人可以指导我正确的方向吗?
谢谢
答案 0 :(得分:0)
Mule 4使用数据编织表达式。尝试删除引号,以使其不被视为静态字符串,然后删除嵌套的表达式括号“#[]”:
<http-transform:headers>#[{'Header_Added': vars.header}]</http-transform:headers>
另一个提示,如果您需要在字符串中间访问var,则可以使用$()语法:
<http-transform:headers>#[{'Header_Added': '$(vars.header)'}]</http-transform:headers>