mule post json data to mule endpoint url - mule

<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" contentType="application/x-www-form-urlencoded" path="getDetails" />
<object-to-string-transformer doc:name="Object to String"/>
<logger message=" Logging ...... #[message.payload]" level="INFO" doc:name="Logger"/>
<logger level="INFO" message=" Logging mule.......#[message.payloadAs(java.lang.String)]" doc:name="Logger"/>
<logger level="INFO" message="Logging demo .......... #[payload]" doc:name="Logger"/>
i invoke thru chrome rest client
http://localhost:8081/getDetails
METHOD : post
below is json data
{
"id": "1", "firstName": "xyz", "lastName": "abc", "address":"xy"
}
whatever json data i passed , i just want to print for processing . but In logging it shows null payload
any correct solutions are welcome

Strange, but I tried your scenario by following :-
<flow name="Flow1" doc:name="Flow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" contentType="application/x-www-form-urlencoded" path="getDetails" />
<object-to-string-transformer doc:name="Object to String"/>
<logger message=" Logging ...... #[message.payload]" level="INFO" doc:name="Logger"/>
<logger level="INFO" message=" Logging mule.......#[message.payloadAs(java.lang.String)]" doc:name="Logger"/>
<logger level="INFO" message="Logging demo .......... #[payload]" doc:name="Logger"/>
</flow>
And you can see I am able to log the values :-

Your code is absolutely correct. As Anirban pointed out, you may be making mistake while invoking the service. I tried invoking this service using postman plugin in Chrome and selected post with raw XML and it worked fine. Please try with postman and confirm if you still have the same issue.

Related

Mule flow not able to read the inbound attachmnet

I have this mule flow , where its polling the source folder to read the text file which I am adding as a attachment and sending through REST call , the same attachment I am trying to read in the different flow but inbound attachment is coming as null , please have a look into the code and help me on this.
<flow name="createAttachment" doc:description="Reading file and sending as attachment.">
<file:inbound-endpoint path="src/test/resources/in/attachment/" responseTimeout="10000" doc:name="File"/>
<file:file-to-byte-array-transformer doc:name="File to Byte Array"/>
<!-- <set-attachment attachmentName="#[originalFilename]" value="#[payload]" contentType="multipart/form-data" doc:name="Attachment"/> -->
<set-attachment attachmentName="#[originalFilename]" value="#[payload]" contentType="multipart/form-data" doc:name="Attachment" />
<http:request config-ref="HTTP_Request_Configuration" path="attachment/excel" method="POST" doc:name="HTTP"/>
</flow>
<flow name="readAttachment">
<http:listener config-ref="HTTP_Listener_Configuration" path="attachment/excel" allowedMethods="POST" parseRequest="false" />
<set-payload value="#[message.inboundAttachments['myattachment.txt']]" doc:name="Retrieve Attachments"/>
<set-payload value="#[payload.getInputStream() ]" doc:name="Get Inputstream from Payload"/>
<file:outbound-endpoint path="src/test/resources/out/attachment" responseTimeout="10000" doc:name="File" outputPattern="#[server.dateTime.toString()].pdf"/>
</flow>
I used the following:
<flow name="readAttachment">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/" allowedMethods="POST" parseRequest="false" doc:name="HTTP" />
<byte-array-to-string-transformer doc:name="Byte Array to String"/>
<logger message="#[payload]" level="INFO" doc:name="Logger" /><file:outbound-endpoint path="src/test/resources" connector-ref="File" responseTimeout="10000" doc:name="File"/>
</flow>
When the attachment was received it was automatically parsed to be the payload, so it was just a case of turning the byte array to string.
I hope this helps

Mule http request logging

I'm sending a https request to a third party api and getting back a response of 403. How do I log to the console the request? I want to verify what I'm sending since it works when I curl it. Using Mule 3.7.0
<flow name="EmailFlow" >
<jms:inbound-endpoint queue="outbound.queue" doc:name="email outbound communicationQ" connector-ref="Active_MQ" >
</jms:inbound-endpoint>
<json:object-to-json-transformer doc:name="Object to JSON"/>
<logger message="msg is: #[payload]" level="INFO" doc:name="Logger"/>
<http:request config-ref="https_request_config" path="/api/v1/transmissions" method="POST" doc:name="HTTP">
<http:request-builder>
<http:header headerName="Authorization" value="somekey"/>
<http:header headerName="Content-Type" value="application/json"/>
</http:request-builder>
<http:success-status-code-validator values="403"/>
</http:request>
<logger level="INFO" doc:name="Logger"/>
</flow>
In 3.7 + You can add the following to your log4j2.xml which performs HTTP wire logging:
<AsyncLogger name="org.mule.module.http.internal.HttpMessageLogger" level="DEBUG" />
<AsyncLogger name="com.ning.http" level="DEBUG" />

Anypoint mule: CopyOnArraylist in output while writing to file with scatter-gather element

The problem statement is to do multiple things parallely and aggregate the response and store it in a file.
link to the mule flow image as in studio:
image
In this flow, what I was trying to do was to set two constant strings in two branches of scatter and gather and aggregate and store in file. I tried overwriting the payload with a "set payload" with "my response". I am expecting "my response" as the content of the file. But instead the file content is:
¨Ìsr)java.util.concurrent.CopyOnWriteArrayListx]ü’F´ê√xpwtmsg 1tmsg 2x
I did debug and the payload at File endpoint was "my response". How and why is the collection getting written into file.
Can anyone help me to get it working.
Following is the xml:
<flow name="mule-assignFlow21123">
<quartz:inbound-endpoint jobName="dummyflow" repeatInterval="10000" responseTimeout="10000" doc:name="Quartz">
<quartz:event-generator-job/>
</quartz:inbound-endpoint>
<scatter-gather doc:name="Scatter-Gather1" >
<threading-profile maxThreadsActive="1" poolExhaustedAction="RUN"/>
<processor-chain>
<set-payload value="msg 1" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger"/>
</processor-chain>
<processor-chain>
<set-payload value="msg 2" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger"/>
</processor-chain>
</scatter-gather>
<set-payload value="my response" doc:name="Set Payload"/>
<file:outbound-endpoint path="/Users/premkumar/Desktop" outputPattern="Results.txt" responseTimeout="10000" mimeType="text/plain" doc:name="Save 2 File"/>
</flow>
The flow will automatically determine the processingStrategy from the in-flight event which will be async because of the quartz endpoint, so the file endpoint will fire async also.
Instead explicitly set the flow's processingStrategy to synchronous:
<flow name="mule-assignFlow21123" processingStrategy="synchronous">
<quartz:inbound-endpoint jobName="dummyflow" repeatInterval="10000" responseTimeout="10000" doc:name="Quartz">
<quartz:event-generator-job/>
</quartz:inbound-endpoint>
<scatter-gather doc:name="Scatter-Gather1" >
<threading-profile maxThreadsActive="1" poolExhaustedAction="RUN"/>
<processor-chain>
<set-payload value="msg 1" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger"/>
</processor-chain>
<processor-chain>
<set-payload value="msg 2" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger"/>
</processor-chain>
</scatter-gather>
<set-payload value="my response" doc:name="Set Payload"/>
<file:outbound-endpoint path="/Users/premkumar/Desktop" outputPattern="Results.txt" responseTimeout="10000" mimeType="text/plain" doc:name="Save 2 File"/>

mule set http header values from postman rest client

mule code
<flow name="getDetails">
<http:inbound-endpoint doc:description="This endpoint receives an HTTP message." doc:name="HTTP" exchange-pattern="request-response" host="localhost" port="8081" path="getDetails"/>
<json:xml-to-json-transformer doc:name="XML to JSON" mimeType="text/json" ignoreBadInput="true"/>
<logger message="xml to json output #[message.payload]" level="INFO" doc:name="Logger"/>
<logger message=" custom header username ... #[message.outboundProperties.get('http.headers.username')]" level="INFO" />
<logger message=" custom header username ... #[message.inboundProperties.get('http.headers.username')]" level="INFO" />
<http:outbound-endpoint exchange-pattern="request-response" address="http://localhost:8081/callReservation" method="POST" contentType="application/json" doc:name="HTTP"/>
</flow>
Here i give input as employee xml in the body
and in header i give username=user1 password=pwd1 in postman rest client
<?xml version="1.0" encoding="UTF-8"?>
<employee>
<name>abc</name>
<address>add1</address>
<phone>1212</phone>
</employee>
I got like error like below
com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '{' (code 123) in prolog; expected '<'
at [row,col {unknown-source}]: [1,1] (javax.xml.transform.TransformerException) (org.mule.api.transformer.TransformerException). Message payload is of type: String
using header and xmlto json conversion is mandatory for me.
can anyone help me out
Following code works fine for me. HTH.
<flow name="getDetails" doc:name="getDetails">
<http:inbound-endpoint doc:description="This endpoint receives an HTTP message."
doc:name="HTTP" exchange-pattern="request-response" host="localhost"
port="1000" path="getDetails" />
<json:xml-to-json-transformer doc:name="XML to JSON"
mimeType="text/json" ignoreBadInput="true" />
<logger message="xml to json output #[message.payload]" level="INFO"
doc:name="Logger" />
<logger
message=" custom header username ... #[message.inboundProperties.'http.headers'.username]"
level="INFO" doc:name="Logger"/>
<http:outbound-endpoint exchange-pattern="request-response"
address="http://localhost:1001/callReservation" method="POST"
contentType="application/json" doc:name="HTTP" />
</flow>
<flow name="testFlow1" doc:name="testFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="1001" path="callReservation" doc:name="HTTP"/>
<json:json-to-xml-transformer doc:name="JSON to XML"/>
<logger message="Data received : #[payload]" level="INFO" doc:name="Logger"/>
</flow>
I am getting the value using following :-
<logger message=" custom header username ... #[message.inboundProperties['username']]"
level="INFO" doc:name="Logger"/>
You need to pass the XML in the body of postman
UPDATE
I am getting the value as below :-
Use the data mapper after the http:outBound to convert to json response.

Can we call a sub-flow or private flow dynamically without using a choice router?

Suppose I have a main flow and there are several sub-flows. I need to call a particular sub-flow if I have the right parameter without using choice component.
Basically, I want to ask if sub-flows can be parametrized.
Yes you can. Here's an example:
<flow name="testFlow1" doc:name="testFlow1">
<poll doc:name="Poll">
<logger level="INFO" doc:name="Logger" />
</poll>
<set-variable variableName="flowName" value="testFlow2"
doc:name="Variable" />
<flow-ref name="#[flowVars.flowName]" />
</flow>
<sub-flow name="testFlow2" doc:name="testFlow2">
<logger level="INFO" doc:name="Logger" message="sub" />
</sub-flow>