I'm trying pass the queryParams dynamically to request depending from user what put in postman. Here is the transform message with variable name jql (this variable I would like then pass in 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>
And for example if user put only parameter demo I would like only this "demo = demo" etc.
In this case I got 400 like this:
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
CodePudding user response:
That's not a good way to implement it and it doesn't even cover all possible cases. Better make it dynamic. Let's convert vars.QueryParams into a list for easier manipulation and then reduce it into a single strings, adding " and " to separate elements when the accumulator is not empty:
vars.queryParams
filterObject (sizeOf($) > 0)
pluck ((value, key, index) -> {k: key, v:value})
map ($.k "=" $.v)
joinBy " and "
With this method you don't even need to know if it contains a specific string.