如何从“listener”组件的查询参数中获取值并将其应用于 Mulesoft Anypoint Studio 中“request”组件的查询参数?

问题描述 投票:0回答:1

我有一个 API 端点,我可以发送

GET
请求以在 Anypoint
Request
组件中使用。它有
start_date
end_date
作为查询参数。

我还有一个

Listener
组件,它使用相同的查询参数为 API 提供服务。

当我将值硬编码到

Request
组件的查询参数时,但我希望它使用
Listener
组件中的值。

当我尝试在侦听器路径中使用

#[attributes.queryParams
对其进行参数化时,我收到一条错误消息,指出 # 是一个坏字符。

如何让监听器查询参数利用响应组件的查询参数。

这是 XML:

<flow name="SystemSleepData" doc:id="59534418-29cb-4830-a5fb-15ac34c6f02d" >
<http:listener doc:name="Listener" doc:id="d47d3e45-05cb-44f8-899b-d6c34fbb2a65" config-ref="HTTP_Listener_config" path="/system_sleep_data" allowedMethods="GET"/>
<http:request method="GET" doc:name="Oura Sleep Info" doc:id="04e89eeb-0125-4d0c-8272-9bc07a47e500" config-ref="HTTP_Request_configuration" path='/v2/usercollection/sleep?start_date="#[attributes.queryParams.start_date]"&amp;end_date="#[attributes.queryParams.end_date]"' >
<http:headers ><![CDATA[#[output application/java ---{"Authorization" : "Bearer HIDDEN"}]]]>
</http:headers>
</http:request>
</flow>

这是组件图片:

http mulesoft anypoint-studio anypoint-platform
1个回答
0
投票

HTTP 监听器接收到的查询参数位于属性

attributes.queryParams
中,它是一个对象,其键是查询参数名称。例如,对于请求
http://localhost:8081/query?start=123&end=456
,您可以使用 DataWeave 表达式
#[attributes.queryParams.end]
来获取值
456

有关 HTTP 侦听器文档的更多详细信息。

在 HTTP 请求程序中,您可以通过使用表达式创建对象来添加查询参数,同样,键是参数名称,值是查询参数值。您可以引用属性或任何有效 DataWeave 表达式中的值。不要将查询参数放在 URL 中,因为这不允许连接器知道这些参数,并且不会将 HTTP 连接器功能用于查询参数。

示例:

<http:request method="GET" config-ref="HTTP_Request_configuration" path="/testpath">
    <http:query-params ><![CDATA[#[output application/java
---
{
    "start" : 123,
    "end" : attributes.queryParams.end
}]]]>
    </http:query-params>
</http:request>

请参阅请求者文档了解更多详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.