How to retrieve data using a db connector in ESB - mule

I am trying to retrieve names of employees from the database by calling a stored procedure and logging it.
But Instead of the names , this is the output of the log:
INFO 2019-12-11 11:48:39,994 [[MuleRuntime].cpuLight.10: [testrun].testrunFlow.CPU_LITE #3bc66a50] [event: 0-12970c00-1bde-11ea-92a5-38dead78b440] org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
org.mule.runtime.core.internal.message.DefaultMessageBuilder$MessageImplementation
{
payload=org.mule.extension.db.internal.result.statement.CloseableMap#8194c34
mediaType=application/java; charset=UTF-8
attributes=<not set>
attributesMediaType=*/*
}
what changes can I make so that I recieve the list of names from db.

your database connector by defaults returns a resultset. convert your resultset into a json by adding a transformer after DB call and set output type to json. if you want only one particular field then you need to have your transform done accordingly
as you can see in your logger that its mentioned the payload type
payload=org.mule.extension.db.internal.result.statement.CloseableMap#8194c34
that is the internal representation of the result set read back from the stored procedure. so just convert it into json or whatever format you want. Put a transform message component after DB call and do the following:
%dw 2.0
output application/json
---
payload
if you want any particular field or something then you can see how your payload is getting structured and do the dataweave accordingly

Related

Mule 4.4 convert Binary payload into JSON

So I have gone through a number of questions related to this topic and tried the various options but none of them seem to work .
Environment - Mule runtime 4.4 , Anypoint Studio 7.11.1
Our limitation is we are not using Enterprise edition only community edition
So cannot use Transform component . However can use dataweave in setPayload / set Variable
I am receiving payload as Binary : see below :
Data appears as JSON but is Binary
I tried changing to JSON in 'setPayload' :
%dw 2.0
import * from dw::core::Binaries
output application/json
---
fromBase64(payload)
However I get this exception :
""Unexpected character 'ÿ' at payload#[1:30] (line:column), expected
Expecting end of input but got `ÿ, while reading `payload` as Json.
1| {"Employee":{"EName":"abcd"}}
^" evaluating expression: "%dw 2.0 import * from dw::core::Binaries output application/json
--- fromBase64(payload)"."
Note - also tried to check datatype of payload using :
typeOf(payload)
This too gave the same exception as above ...
Please help
Update 1:
Based on comments from #aled few additional details :
I am making a GET request to my api endpoint which is reading data from an ERP system which returns data in XML
Then I use a custom component that is being used to transform the XML data to JSON
The o/p of this custom component is what is generating binary data which I am unable to parse
I agree regarding Base64 that was useless
I later even tried with Transform component and all of the below attempts failed with the same exception :
Attempt#1:
%dw 2.0
output application/json
---
if (isEmpty(payload.^raw)) {
"payload": "No payload!!"
}
else {
"payload": payload
}
Attempt#2:
%dw 2.0
output application/json
---
payload
The above XML payload is a short excerpt received from ERP
I need to parse the payload and identify if there is an error present in the payload .
NOTE : if I simply log the payload it all shows up fine in the logs.
is it not possible to convert binary data to json ? do you think there is something wrong in the data received either from ERP or in the XML to JSON Transformer ?
Update#2
So based on earlier comments from #aled ( thanks ! ) started focusing on payload o/p of the ERP component ( which is XML ) BEFORE it is fed to our custom transformer ( which is generating binary data )
Attaching screen print for clarity and will also paste non proprietary code :
Now when I inspect the payload I can see it as :
<?xml version = "1.0" encoding="utf-8" ?>
<Output>
<ErrorDetails>
<Status>1</Status>
<Details>Invalid user or organisation</Details>
</ErrorDetails>
<Employee>
<EName>abcd</EName>
</Employee>
</Output>
and in debug mode I can see that payload is String and NOT Binary
Now I can access individual elements of the payload .
Example:
<set-variable value="#[output application/java --- payload[0].ErrorDetails['Status']]"
doc:name="extract status" variableName="status"/>
So this gives me the value 1 which is expected and good .
Conclusions:
1 The component that interacts with ERP is generating Non Binary / stream data
2 This data as expected is parseable - all good here
3 AFTER this data is fed to our custom component - the o/p payload is now binary and am running into issues parsing it ...
Question:
I know this being a custom component i cannot share code details here and thereby cannot expect inputs BUT regarding #aled comments about hexdump do you think I should try and use it against the output of the custom component ?
Once again a big thanks for your patience and help
Using function fromBase64() is just wrong. The value is not a Base64 string. It doesn't makes sense to try it. Using toBase64() would be useless because you would not be able to do anything with it, other than sending it somewhere else.
Your payload seems to be a JSON, and the media type is application/json in your screenshot, but it contains an UNICODE character at the end at least. You need. What are you trying to do with that payload exactly?
If the payload is truly a binary there is nothing you can do about that in the application. If you are receiving it from somewhere else check with the source. You didn't provide details of the source. Also ensure it is not your application corrupting the payload somehow.

payload changing to com.mulesoft.weave.reader.DefaultSeekableStream (Mule 3)

Payload in muleflow is getting converted in com.mulesoft.weave.reader.DefaultSeekableStream from org.glassfish.grizzly.utils.BufferInputStream. I am not sure why and due to this the subsequent activities (dw transformer) is failed. Anyone have any idea what could be the reason and how to overcome this.
The payload is pretty simple and I am trying to use two transformation activity, In first activity I parse the payload and create a flowVariable and in second transformation is to add values in payload. However, after first transformation the payload is changed to DefaultSeekableStream.
Thanks,
Abhi
Error message:

How to accept any multipart file (image/pdf/csv) from postman and create the same file into SFTP location in Mule 4?

When I tried this usecase for some MIME types the content is coming in payload.parts[0].content, for some MIME types the content is coming in payload.parts.file.content. How to create the file in the same MIME type as how we received from postman in SFTP?
Quite not sure in what ways you have used and tested the both expression. Please find the screenshot below. I have added different format of datas in single request. If you use mutiple format in single request and wanting to extract individually. Use payload.parts[0].content and payload.parts[1].content
Note: This expression ideally mean whatever you add (form-data) in postman body request can be extract using index.
Example: if you have added content-type: application/xml in the below picture 3rd row, should be using payload.parts[2].content
If you're using single request payload.parts.file.content can be used.
Once after the request reached HTTP/HTTPS ( source listener), use dataweave as below to convert binary in to the defined structure data
%dw 2.0
output multipart/form-data
---
payload
After extracting the contents using the expressions, you can write in to SFTP (Screenshot) here I have stored in Vars (based on your logic)

Parsing XML Response In GateWayScript

Hi i am new to API connect ... i have a use case where i have to merge responses coming from two endpoints in XML format based on certain conditions.
My flow in the assemble section is like this
1) INVOKE
(i make my
first service call
and capture the response
in a custom 'Response
object varibale' -XMLResponse1
2) INVOKE
(i make my
second service call
and here i am not using any
custom 'Response object varibale'
Instead i am using apim.getvaribale('message.body') to get the response
3)GATEWAYSCRIPT
Here i want to write my script for parsing the xml and merging the two responses
and send back the merged response to the consumer
I observed that the xml response is not getting captured in a custom Response object variable when i try to capture it like below
var test1= apim.getvariable('XMLResponse1');
test1.item(0).childNodes
it throws me an exception like this
test1.item is not a function
now for the second response like below where i am not capturing the response in a custom Response object variable it works well
var test2= apim.getvariable('message.body');
My question:
1)How do i capture the xml responses in a custom Response object variable?
2)How can i parse the response into a javascript object? are there any libraries supported in api connect?
Below is the samples found from IBM community. Hope this may help you.
**** Sample XML ****
<Routing>
<partner name="Partner A" key="1">
<from_ID>PartnerA-KEY1-INT</from_ID>
<to_ID>PartnerA-KEY1-EXT</to_ID>
<destination>PartnerA-KEY1-DESTINATION</destination>
</partner>
<partner name="Partner B" key="2">
<from_ID>PartnerB-KEY2-INT</from_ID>
<to_ID>PartnerB-KEY2-EXT</to_ID>
<destination>PartnerB-KEY2-DESTINATION</destination>
</partner>
<partner name="Partner C" key="3">
<from_ID>PartnerC-KEY3-INT</from_ID>
<to_ID>PartnerC-KEY3-EXT</to_ID>
<destination>PartnerC-KEY3-DESTINATION</destination>
</partner>
</Routing>
**** Corresponing Gateway Script *****
var response = apim.getvariable('XMLResponse1.body');
var objType = response.item(0);
var string = objType.getElementsByTagName("partner").item(0).getElementsByTagName("from_ID").item(0).textContent;
output ---> string = PartnerA-KEY1-INT
Why do you want to merge them in a GatewayScript node??
You could merge them in a mapping node, in wich you have 2 variables as input (refered to the output objects of your invokes) and one XML object as output...
If you have to apply some conditions or comparisons, you could do them in the code part of the mapping nodes

Mule:Extracting parent flow message in subflow/synchronous flow

Is there a way in mule to extract parent flow message in sub/synchronous flow after sub message source has generated its message and modify it.Sub flow is having its own message source.
You can always get the message of parent flow in sub flow ..
Now extracting a message depends on its data type ...
For example if the payload type is xml and you need to extract particular value of a node, you can use XPATH...
Similarly for JSON payload there is different techniques of extracting it..
And now if you want the entire payload of parent flow and not just part of it, then you can always use #[message.payload] expression in subflow