Sumved Shami
Sumved Shami

Reputation: 102

Passing dynamic query params and path params in http request connector

I am looking to pass list of query params and path params to http request mule connector. The input which is coming to this connector is a List of pojo which contains the value of query and path params.

The http request connector looks something like below:

<http:request config-ref="HTTP_Request_Configuration" path="/rates/api/v1/rates/{in}.csv" method="GET" doc:name="End_HTTP request">
<http:request-builder>
    <http:query-param paramName="api_key"
        value="abcde" />
    <http:query-param paramName="quote" value={target_currency} />
    <http:query-param paramName="date" value={date} />
    <http:query-param paramName="fields" value="averages" />
    <http:uri-param paramName="in" value={source_currency} />
</http:request-builder>

The pojo class looks like this:

public class Data {

    private String sourceCurrency;
    private String targetCurrency;
    private String date = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());

    public Data() {
    }

    public String getSourceCurrency() {
        return sourceCurrency;
    }

    public void setSourceCurrency(String sourceCurrency) {
        this.sourceCurrency = sourceCurrency;
    }

    public String getTargetCurrency() {
        return targetCurrency;
    }

    public void setTargetCurrency(String targetCurrency) {
        this.targetCurrency = targetCurrency;
    }

    public String getDate() {

        return date;
    }
}

If the input to http request connector is the object of Data class then how to set the query and path params.

Can someone please help me with an example?

Thank you.

Sumved

Upvotes: 2

Views: 4447

Answers (2)

Ismael G.
Ismael G.

Reputation: 378

I think you need to add a Data Object in your flow (a flow Variable). Then you can use the variable in your request builder, like {flowVars.variable.someproperty}

Upvotes: 1

Manuel Saucedo
Manuel Saucedo

Reputation: 525

First you have to assign the value from the object Data (which is the payload) to the required variables (targetCurrency, date, sourceCurrency) and then you have to assign the value of the variables to the query params and path params. Here you have an example:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:file="http://www.mulesoft.org/schema/mule/file"
      xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"
      xmlns:db="http://www.mulesoft.org/schema/mule/db"
      xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml"
      xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
      xmlns:json="http://www.mulesoft.org/schema/mule/json"
      xmlns:ws="http://www.mulesoft.org/schema/mule/ws"
      xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps"
      xmlns:email="http://www.mulesoft.org/schema/mule/email"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"      
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
        http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd
        http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
        http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
        http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
        http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
        http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
        http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
        http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
        http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
        http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
        http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd
        http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.xsd
        http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd       
    ">

    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration">
        <http:worker-threading-profile maxThreadsActive="64" />
    </http:listener-config>

    <http:request-config name="HTTP_Request_Configuration" host="localhost" port="8081"/>

    <flow name="testingFlowService">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/service/{waitTime}/*" doc:name="HTTP"/>
        <logger message="Query Params: #[message.inboundProperties.'http.query.params']" level="INFO" doc:name="Log Failure"/>
        <logger message="Uri Params: #[message.inboundProperties.'http.uri.params']" level="INFO" doc:name="Log Failure"/>
    </flow>

    <flow name="testingFlowClient">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/client/*" doc:name="HTTP"/>
        <logger message="Before Transformation" level="INFO" />
        <json:json-to-object-transformer returnClass="com.testing.domain.GeneralRequest" />
        <set-variable variableName="processTypeJob" value="#[payload.processTypeJob]"/>
        <set-variable variableName="waitTime" value="#[payload.waitTime]"/>
        <logger message="After Transformation" level="INFO" />
        <json:object-to-json-transformer/>
        <http:request config-ref="HTTP_Request_Configuration" path="/service/{waitTime}" method="POST" 
                        responseTimeout="50000000">
            <http:request-builder>
                <http:query-param paramName="api_key"
                    value="abcde" />
                <http:query-param paramName="processTypeJob" value="#[processTypeJob]" />
                <http:query-param paramName="fields" value="averages" />
                <http:uri-param paramName="waitTime" value="#[waitTime]" />
            </http:request-builder>
        </http:request>
    </flow>

</mule>

And this is the class I'm using:

package com.testing.domain;

import java.util.List;

public class GeneralRequest {
    private String processTypeJob;
    private String waitTime;
    private List<String> processIds;
    private String processTypeGroup;
    private List<JobConfiguration> jobConfigurations;

    public String getProcessTypeJob() {
        return processTypeJob;
    }
    public void setProcessTypeJob(String processTypeJob) {
        this.processTypeJob = processTypeJob;
    }
    public List<String> getProcessIds() {
        return processIds;
    }
    public void setProcessIds(List<String> processIds) {
        this.processIds = processIds;
    }
    public String getProcessTypeGroup() {
        return processTypeGroup;
    }
    public void setProcessTypeGroup(String processTypeGroup) {
        this.processTypeGroup = processTypeGroup;
    }
    public List<JobConfiguration> getJobConfigurations() {
        return jobConfigurations;
    }
    public void setJobConfigurations(List<JobConfiguration> jobConfigurations) {
        this.jobConfigurations = jobConfigurations;
    }
    public String getWaitTime() {
        return waitTime;
    }
    public void setWaitTime(String waitTime) {
        this.waitTime = waitTime;
    }



}

For more information you can see the next link: https://docs.mulesoft.com/mule-user-guide/v/3.7/http-request-connector

Upvotes: 3

Related Questions