我是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 4使用dataweave表达式。尝试删除引号,使其不被视为静态字符串,并删除嵌套的表达式括号'#[]':
<http-transform:headers>#[{'Header_Added': vars.header}]</http-transform:headers>
或者另一个提示,如果你需要访问字符串中间的var,你可以使用$()语法:
<http-transform:headers>#[{'Header_Added': '$(vars.header)'}]</http-transform:headers>