1

In mule4 at RequestProcessor we set some hedaers and queryparams. eg: for headers;

output application/java
---
{
    "xXX- applicationid" : vars.'applicationid',
    "XX-username" : p('username'),
    "XXX-signature":vars.'signature'
}


            <http:request method="GET" doc:name="HTTP-Get"
                doc:id="ec8910c5-36fe-4367-9234-d459776ac8e3" config-ref="request_config"
                path="${auth.path}">
                <http:headers><![CDATA[#[output application/java ---
   {
        "xXX- applicationid" : vars.'applicationid',
        "XX-username" : p('username'),
        "XXX-signature":vars.'signature'
    }
]]]></http:headers>

            </http:request>

This works fine. But in future, if we want to add another header parameter, we need to come here and edit this config. I don't want to do it here.. I just want to pass all my headers as a variable /component here..So, i don't need to edit this connectors in future. rather, I could do at that variable/component setting level and we can reuse this connectors .

How to do this in mule4 for more extensible/manageable connectors?

Ratha
  • 9,434
  • 17
  • 85
  • 163

2 Answers2

1

Don't use http connector directly. Wrap it to separate flow or subflow and use flow reference to this flow. In this case it will be only one http connector which you can change/modify in one place.

Here is example - one helloFlow makes two http requests to echoFlow and echo1Flow via reference to one SingleHttpRequestFlow.

SingleHttpRequestFlow is the only one place where http request is made. So, if change of headers is required then it is done in one place. enter image description here

here is the code

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:wsc="http://www.mulesoft.org/schema/mule/wsc" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"

 xmlns:http="http://www.mulesoft.org/schema/mule/http"

 xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/wsc http://www.mulesoft.org/schema/mule/wsc/current/mule-wsc.xsd">

 <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="3cca14aa-10d5-465d-9ea3-523c3b927828" >

  <http:listener-connection host="0.0.0.0" port="8081" />

 </http:listener-config>

 <http:request-config name="HTTP_Request_configuration" doc:name="HTTP Request configuration" doc:id="2149cc85-f8a5-43c3-b940-b12ea0dc5934" >

  <http:request-connection host="localhost" port="8081" />

 </http:request-config>
    <sub-flow name="SingleHttpRequestFlow" doc:id="51814424-61c3-4abb-8548-dfd46605beaa" >
        <http:request method="GET" doc:name="Request" doc:id="344daf1e-248a-4bca-a829-1c00929d835d" url="#[vars.url]">
            <http:headers><![CDATA[#[output application/java
---
{
    "Key" : "Value",
    "Key-1" : "Value-1"
}]]]></http:headers>
        </http:request>
    </sub-flow>
    <flow name="echoFlow" doc:id="20f7749a-dd1f-43b9-a92f-ced0f17471ed" >
        <http:listener doc:name="Listener" doc:id="4c85f760-96fe-4072-af2a-f74bcd514d8b" config-ref="HTTP_Listener_config" path="/echo"/>
        <ee:transform doc:name="Transform Message" doc:id="e1942d41-468f-4b5c-9f78-d54f4a5876d8" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
attributes.headers ++ attributes.queryParams]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger" doc:id="94eae8db-f453-4dc3-bbad-43f1fee47063" />
    </flow>
    <flow name="echo1Flow" doc:id="1d735acd-b39a-430d-aeed-1b113332a970" >
        <http:listener doc:name="Listener" doc:id="3e7ec930-7784-420e-b71a-cd8553524f42" config-ref="HTTP_Listener_config" path="/echo1"/>
        <ee:transform doc:name="Transform Message" doc:id="893f3c97-d8ed-43fe-95ef-d35175b88c5d" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
attributes.headers ++ attributes.queryParams]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger" doc:id="929cd9a2-3b1d-4ddf-badc-ef38f88f10eb" />
    </flow>
    <flow name="helloFlow" doc:id="3f60bcae-83a5-46ce-8508-2a29bc787719" >

  <http:listener doc:name="Listener" doc:id="1faf3649-d661-45aa-b7a5-fc573029cba5" config-ref="HTTP_Listener_config" path="/test" allowedMethods="GET">

   <ee:repeatable-file-store-stream inMemorySize="1024" />

   <http:response reasonPhrase="Success" >

    <http:headers ><![CDATA[#[output application/java

---

{

 name : "Max"

}]]]></http:headers>

   </http:response>


</http:listener>
        <ee:transform doc:name="echo" doc:id="b3c02cdc-25dd-4046-9519-c9cfc3db4373" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="url" ><![CDATA[%dw 2.0
output application/java
---
'http://localhost:8081/echo?echo=one']]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <flow-ref doc:name="SingleHttpRequestFlow" doc:id="c66ea21d-361d-4ef1-8ca1-915798d07ddf" name="SingleHttpRequestFlow" />
        <logger level="INFO" doc:name="Logger" doc:id="08eeb07b-d209-40cf-82e8-50fa46fbf57f" message="#[payload]"/>
        <ee:transform doc:name="Transform Message" doc:id="6e7d212f-c57e-419a-a430-cc919082b945" >
            <ee:message >
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="url" ><![CDATA[%dw 2.0
output application/java
---
'http://localhost:8081/echo1?echo=two']]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <flow-ref doc:name="SingleHttpRequestFlow" doc:id="74a29771-a876-4177-ac8a-7401af2051ee" name="SingleHttpRequestFlow"/>
        <logger level="INFO" doc:name="Logger" doc:id="7769318d-41da-4737-a72a-5f0b7bc43496" message="#[payload]"/>


</flow>

</mule>
Alex
  • 4,457
  • 2
  • 20
  • 59
  • I didnt get this,I have edited bit more with my quetsion. I dont want to edit my component in future, if there is additional http header involved. Rather would like to pass all heders as a component..does mule has such option using processor? – Ratha May 12 '20 at 07:05
  • 1
    provided example. You wanted one place to change headers. here you are. – Alex May 12 '20 at 12:29
1

You can just send the object instead of the expression. You can generate that object dynamically, with any DataWeave expressions. Note that your original example is just a DataWeave expression that returns an object, only that it is hard coded inside the HTTP Request.

For example, I'll assign an object to a variable, then use that as the headers.

<set-variable variableName="myHeaders" value="#[output application/java --- { myHeader1:  'header1, myHeader2: 'header2' }]" />

<http:request method="GET" doc:name="Request" config-ref="HTTP_Request_configuration" path="/headers">
   <http:headers ><![CDATA[#[output applicaton/java
     ---
     vars.myHeaders
   ]]]></http:headers>    
</http:request>
aled
  • 21,330
  • 3
  • 27
  • 34
  • Thanks that is what i wanted. So same approach I can use for query parameters etc..? – Ratha May 12 '20 at 23:21
  • 1
    It seems to be the same. Check the example at https://docs.mulesoft.com/connectors/http/http-connector-xml-reference#http-request-response-and-error-response-body – aled May 13 '20 at 16:59