How do I take values from query parameter of `listener` component and apply it to query parameter of `request` component in Mulesoft Anypoint Studio?

I have an API endpoint that I can send GET request to using in Anypoint Request component. It has start_date and end_date as query parameter.

I also have a Listener component that is serving the API using the same query parameters.

When I hardcode the values to the query parameters of my Request component but I want it to use the values from the Listener component.

When I try to parameterize it using #[attributes.queryParams in the path of the listener, I get a error saying that # is a bad character.

What can I do to have the Listener query parameters utilize the query parameters of the Response component.

Here’s the 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>

Here’s a picture of the components:

enter image description here

Query parameters received by the HTTP Listener are in attribute attributes.queryParams which is an object and its keys are the query parameter names. For example for the request http://localhost:8081/query?start=123&end=456 you can use the DataWeave expression #[attributes.queryParams.end] to get the value 456.

More details on the HTTP Listener documentation.

In the HTTP Requester you can add query parameters by using an expression to create an object, again the keys are the parameter name and the values the query parameter value. You can reference the values from the attributes or any valid DataWeave expression. Don’t put the query parameters in the URL because that doesn’t allow the connector to know those are parameters and doesn’t use the HTTP Connector features for query parameters.

Example:

<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>

See the Requester documentation for more details.

Leave a Comment