Reputation: 275
I am passing an array in an API as an argument to a get request. I new to pass this array as a query parameter to an HTTP Requester.
I am using flow variable for string and numbers. But I don't know what to use for Arrays.
I saw some examples using the foreach scope and tried that but I was told that I can not use foreach in http:request-builder. Please is there a work around this? I am new in Mulesoft. Thanks
illustration
https://apiEndPoint/api/Get?param1=Americas¶m2=00MA¶m3=Disruption¶m3=SomethingElse
param1 and param2 are captured with flow variables and sent with the HTTP Requester
Using flow variable for param3 overwrites Disruption with Something else
<http:request-builder>
<http:query-param paramName="param1" value="#[flowVars.param1]" />
<http:query-param paramName="param2" value="#[flowVars.param2]" />
<http:query-param paramName="param3"value="#[flowVars.param3]" />
</http:request-builder>
Upvotes: 0
Views: 2535
Reputation: 275
When setting up the http requester, I added query parameters and this expression
#[message.inboundProperties['http.query.params']]
And it fixed the issue for me. I didn't have to set the variables again.
<http:request-builder>
<http:query-params
expression="#[message.inboundProperties['http.query.params']]" />
</http:request-builder>
Upvotes: 0
Reputation: 25872
One solution is to create a map from the list, with keys 'param1', 'param2', etc. The HTTP request builder allows to use a map to generate all the query params, using the keys as the names. To create the map I used DataWeave:
<flow name="test-array-query-paramFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<set-variable variableName="myArray" value="#[ [ 'Americas','00MA', 'Disruption', 'SomethingElse' ] ]" doc:name="Variable"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-variable variableName="params"><![CDATA[%dw 1.0
%output application/java
---
( flowVars.myArray map {
("param" ++ ($$ as :number +1)) : $
} ) reduce ((val, acc = {}) -> acc ++ val)
]]></dw:set-variable>
</dw:transform-message>
<logger message="payload #[flowVars.params]" level="INFO" doc:name="Logger"/>
<http:request config-ref="HTTP_Request_Configuration" path="/api" method="GET" doc:name="HTTP">
<http:request-builder>
<http:query-params expression="#[flowVars.params]"/>
</http:request-builder>
</http:request>
</flow>
Enabling HTTP wire logging we can confirm the params are generated as expected:
GET /api?param1=Americas¶m2=00MA¶m3=Disruption¶m4=SomethingElse HTTP/1.1
Upvotes: 0