Deleting headers from request-config instead of flow in MULE 4 - mule

I'm trying to find solution for deleting request headers in MULE 4 by more effective way then actual.
In request config is possible to have <http:default-headers />, but I can't find way to delete headers from here. So I'm deleting headers in flow ->
like this:
<http:headers>#[attributes.headers -- ["host", "http.headers"]]</http:headers>
Full version of flow is here:
<flow name="WS_name">
<http:listener path="someway/*" config-ref="External_Listener" doc:name="HTTP">
<http:response statusCode="#[vars.httpStatus default 200]">
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:response>
<http:error-response statusCode="#[vars.httpStatus default 500]">
<http:body>#[payload]</http:body>
<http:headers>#[vars.outboundHeaders default {}]</http:headers>
</http:error-response>
</http:listener>
<http:request method="#[attributes.method]" sendBodyMode="AUTO" config-ref="HTTPS_Request_configuration_proxy" url="#['https://${endpoint.WS_name.host}:${endpoint.WS_name.port}' ++ '${endpoint.WS_name.path}/']">
<http:headers>#[attributes.headers -- ["host", "http.headers"]]</http:headers>
</http:request>
If anyone have better solution, please, share it.

What you are doing is fine. If you want to do that in the flow outside of the http:request, then you can. All the http:headers expects is a map. So here is an example of using a var:
<set-variable variableName="myAttributes" value="#[attributes.headers -- ['host', 'http.headers']]" />
<http:request method="#[attributes.method]" sendBodyMode="AUTO" config-ref="HTTPS_Request_configuration_proxy"
url="#['https://${endpoint.WS_name.host}:${endpoint.WS_name.port}' +++ '${endpoint.WS_name.path}/']">
<http:headers>#[vars.myAttributes]</http:headers>
</http:request>

Related

Mule : Use dynamic config-ref based on input values

I'm trying to connect to SQL Database with some configuration. But based on the input from the API we are supposed to hit different DB.
As of now, we have the code as
<choice doc:name="Check myFlag">
<when expression="#[flowVars.myFlag == 'true']">
<db:stored-procedure config-ref="Database_Configuration_1" doc:name="DB_config_1">
<db:dynamic-query><![CDATA[#[flowVars.callSPName]]]></db:dynamic-query>
</db:stored-procedure>
</when>
<otherwise>
<db:stored-procedure config-ref="Database_Configuration_2" doc:name="DB_config_2">
<db:dynamic-query><![CDATA[#[flowVars.callSPName]]]></db:dynamic-query>
</db:stored-procedure>
</otherwise>
</choice>
Instead of repeating <db:stored-procedure../> twice, is there a way where I can set a flow var with the DB config reference and use it?
Something like,
<db:stored-procedure config-ref="#[flowvars.db_config]" doc:name="DB_config_2">
<db:dynamic-query><![CDATA[#[flowVars.callSPName]]]></db:dynamic-query>
</db:stored-procedure>
In Mule 3, no. config-ref's are evaluated at application startup, not runtime.
In Mule 4 this is possible using Dynamic Configurations: https://docs.mulesoft.com/mule-sdk/1.1/static-dynamic-configs
Potential Mule 3 solutions documented here: https://help.mulesoft.com/s/article/How-to-configure-connector-with-dynamic-parameters
Put the DB call inside a sub-flow and call it from the rest of the flows with a <flow-ref>.

Unable to set event in Munit

I had started writing a simple Munit test for one of my flow. In the main flow. I have added a Munit set event to set some URI params to the mainflow.
<munit:test name="business-logic-test-suite-mainFlowTest" description="Test">
<munit:set-event cloneOriginalEvent="false">
</munit:set-event>
<set-payload value="#[payload]" doc:name="Set Payload"/>
<flow-ref name="mainFlow" doc:name="Flow-ref to mainFlow"/>
<munit:assert-payload-equals message="Output message is not matching excepted value #[payload]" expectedValue="#[payload]" doc:name="Assert Payload"/>
</munit:test>
When I try to run the test, the studio is throwing an error "Invalid content was found starting with element 'munit:set-event'". I tried some search on the internet and could not find anything related.
Any clue what could be missing ?
Thanks
If you want to set URI params, you can use Set Message component.
Another way to set the URI params is Message Properties component.
You can use this link for your reference.

How to send array in url in mule esb?

I need to send array in url. I have tried different ways like
http://localhost:8081/?RedundedItems[]=[00,11]
http://localhost:8081/?RedundedItems[]=00,11
http://localhost:8081/?RedundedItems=00&RedundedItems=11
http://localhost:8081/?RedundedItems[]=00&RedundedItems[]=11
Please assist me.
Sorry but I really dont think this is possible or even if by some miracle it is, I dont see it as practical...
One solution would be to send your data as normal and maybe process it into an array via datamapper?
Hope I have been of some help.
JB
The following works using your third option:
<flow name="request-testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
<set-payload value="#[message.inboundProperties.'http.query.params'.getAll('RedundedItems')]" doc:name="Set Payload"/>
<object-to-string-transformer doc:name="Object to String"/>
</flow>
So hitting http://localhost:8081/?RedundedItems=00&RedundedItems=11 the output is: [00, 11].
HTH.
The right way is http://localhost:8081/?RedundedItems=00,11 . You can't specify the parameter is an array.

Mule Server 3.6 > Anypoint Studio > Raw JSON as POST data

Still learning Mulesoft's Anypoint Studio... I am confused as how will I be able to access raw JSON POST data via the HTTP Listener then use the Choice flow control to execute conditions based on a value from a given JSON index. Anyone can show/tell me how to do this?
The JSON HTTP body will automatically become the payload of your message in Mule probably represented as Stream.
Just for demo purposes, try logging the payload after your http:listener using:
<object-to-string-transformer />
<logger level="INFO" message="#[payload]" />
There best way to query JSON is to transform it to a Map suing the JSON module transformers.
<json:json-to-object-transformer returnClass="java.util.HashMap" />
And then query it using MEL like standard MVEL or Java syntax.
For a JSON document like: {"person" : {"name" : "bob"}}
<logger message="#[payload.person.name]" level="INFO" />
You can use these expressions in your choic router also:
<choice>
<when expression="#[payload.person.name == 'bob']">
do something ...
</when>
</choice>

How to Extract the Flow Name and MessageProcessor Name using MEL - MULE ESB

I'm not sure, how can we extract the flow-name and message-processor Name through MEL. For example I have multiple message processor. For logging, i need to extract the flow Name and Message-processor, so that I can find out transaction has crossed this particular flow and its message processor. Is there any simple way to find out. Please guide me. Please find the screenshot below. Here i need to Extract - set payload and its flowName (flow1)
Thanks in advance.
For mule 3.8+ version onwards #[flow.name] don't work.
Use #[mule:context.serviceName] expression in logger or component to extract the name of the flow
I know this post is old but I have been trying to find a way to do this in MEL for error handling emails.
For the flow name you can use #[exception.event.flowConstruct.name]
for the failing message processor you can use #[exception.failingMessageProcessor].
Both of these work in MEL without the need to use an flowVar.
Please note however, that the failing processor does not always come back with a value but comes back with null, I'm not sure why.
You can extract the flow-name with MEL : #[flow.name]
<flow name="name" doc:name="name">
<http:inbound-endpoint address="http://localhost:8090/resources" doc:name="HTTP" />
<logger message="name of flow: #[flow.name]" level="INFO" doc:name="Logger"/>
<set-payload value="name" doc:name="Set Payload"/>
</flow>
or
flowConstruct.getName() in a Message Processor
Two ways to acthive this (from current flow name)
First one is -
<logger message="Current flowName: #[flow.name]" level="INFO" doc:name="Logger"/>
and the second one is -
<logger message="Current flowName: #[context:serviceName]" level="INFO" doc:name="Logger"/>