Dataweave 将变量分配给 url mulesoft

Dataweave passing varaible to url mulesoft

我正在尝试动态传递 queryParams 以根据用户放入邮递员的内容进行请求。 这是带有变量名称 jql 的转换消息(这个变量我想然后传入 url)


<ee:transform doc:name="Transform Message" >
            <ee:message>
            </ee:message>
            <ee:variables>
                <ee:set-variable variableName="queryParams" ><![CDATA[
{
    "demo" : attributes.queryParams['demo'] default "",
        "demo2": attributes.queryParams['demo2'] default "",
         "demo3" :attributes.queryParams['demo3'] default ""
}
                
]]></ee:set-variable>
    <ee:transform doc:name="Transform Message"  >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="jql" ><![CDATA[
if(vars.queryParams.demo != null and vars.queryParams.demo2 == null and vars.queryParams.demo3 == null)
    "demo= " ++ vars.queryParams.demo 
else if(vars.queryParams.demo2 != null and vars.queryParams.demo3 == null and vars.queryParams.demo == null)
    "demo2 = " ++ vars.queryParams.demo2 
else if(vars.queryParams.demo != null and vars.queryParams.demo2 != null and vars.queryParams.demo3 == null )
"demo= " ++ vars.queryParams.demo ++ " and demo2 = " ++ vars.queryParams.demo2 

]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <http:request method="GET" doc:name="Request"  config-ref="HTTP_Request_configuration" path="/demo/search">
            <http:query-params><![CDATA[#[output application/java
---
{
    "jql" : vars.jql
}]]]></http:query-params>
        </http:request>

例如,如果用户只输入参数演示,我只想要这个“demo = demo”等

在这种情况下,我得到了这样的 400:

Message               : HTTP GET on resource 'https://demo/search' failed: bad request (400).
Element               : searchForIssuesUsingJQL/processors/2 @ jira:jira.xml:160 (Request)
Element DSL           : <http:request method="GET"
 doc:name="Request"  config-ref="HTTP_Request_configuration" path="demo/search">
<http:query-params>#[output application/java
---
{
    "jql" : vars.jql
}]</http:query-params>
</http:request>
Error type            : HTTP:BAD_REQUEST

这不是实现它的好方法,它甚至没有涵盖所有可能的情况。最好让它动态。让我们将 vars.QueryParams 转换为一个列表以便于操作,然后将其缩减为单个字符串,在累加器不为空时添加“和”来分隔元素:

vars.queryParams
    filterObject (sizeOf($) > 0)
    pluck ((value, key, index) -> {k: key, v:value})
    map ($.k ++ "=" ++ $.v)
    joinBy  " and "

使用此方法,您甚至不需要知道它是否包含特定字符串。