1

I am trying to get information of attached file in mule application.I am using POSTMAN to POST a text file .But After HTTP receiver in mule application Inbound Attachment Names have size=0.How to resolve such scenario?

Anurag
  • 65
  • 2
  • 13

3 Answers3

2

Check this link:

Tool for sending multipart/form-data request

You basically need to select form-data and select File from the drop down then upload the file using the Choose Files button.

You can check the attachment in mule using the mel expression:

'#[message.inboundAttachments]

Here is my updated answer this I can now post a photo:

1.

enter image description here

OR

enter image description here

  1. Configure the Foreach:

enter image description here

  1. The Set-Payload has this value:

'#[org.apache.commons.io.IOUtils.toByteArray(payload.getInputStream());]

  1. The logger has this value:

'#[payload]

  1. Here is a sample postman setup with return code 200, note that I added "key" 1 and 2 which we need:

enter image description here

The result in my console is like this (I removed others):

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm&quot; xmlns:batch="http://www.mulesoft.org/schema/mule/batch&quot; xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper&quot; xmlns:db="http://www.mulesoft.org/schema/mule/db&quot; xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking&quot; xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc&quot; xmlns:file="http://www.mulesoft.org/schema/mule/file&quot; .. .. .. .. <mule xmlns:context="http://www.springframework.org/schema/context&quot; xmlns:cluster="http://www.mulesoft.org/schema/mule/ee/cluster&quot; xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper&quot; .. .. .. ..

Community
  • 1
  • 1
Ralph Rimorin
  • 329
  • 2
  • 10
  • I am using this expression but i am getting null value – Anurag Sep 22 '15 at 18:53
  • Thanks afelisatti. @Anurag, I am sorry I was suppose to send a screenshot of a mule flow but I wasn't since I am new here. In your mule flow, after your HTTP inbound, add a Set Payload transformer with the value #[message.inboundAttachments]. You should be able to fetch the attach file. Then add a transformer (like if attach an XML, then you add an Object to XML; or if the file is a JSON, then use Object to XML). If you want to view the content, add a Object to String and logger. Then you would be able to log the #[payload]. It should display there the content of the file. – Ralph Rimorin Sep 22 '15 at 23:20
  • I am unable to fetch the files as there is no attachment in the request i receive from POSTMAN.size of inbound attachments is showing as 0 – Anurag Sep 23 '15 at 06:56
  • But payload i am getting is null payload. – Anurag Sep 23 '15 at 08:57
  • Now the attachment is coming in inbound properties section but how to get content of file as payload is coming as null – Anurag Sep 23 '15 at 09:04
  • what is the file type or extension of your file? Because in my example I used XML, and JSON is also possible. – Ralph Rimorin Sep 23 '15 at 09:10
  • I have used .txt file – Anurag Sep 23 '15 at 09:13
  • Change the Object to XML to Object to String. see the edited solution number 1. That should work for .xml, .json, .properties, or .txt. – Ralph Rimorin Sep 23 '15 at 09:23
  • Actually due to null payload application is unable to do foreach as foreach is not applicable for null payload – Anurag Sep 23 '15 at 09:25
  • I edited the answer. see.. 2. Configure the Foreach: – Ralph Rimorin Sep 23 '15 at 09:32
  • Thank you very much Ralph.You have saved the day for me.:) – Anurag Sep 23 '15 at 09:40
  • Sorry to bug you again Ralph.But if i have to send this attachment via HTTP POST then how can we do this? If am using copy attachment then error i am getting is 'message is of type byte []' – Anurag Sep 23 '15 at 10:00
  • If you use the HTTP POST and place any data in the body or payload on Postman, you need a Byte Array to Object after the HTTP endpoint. Any transformer the will transform the stream to other object you need. – Ralph Rimorin Sep 23 '15 at 10:02
1

Sender:

<http:listener config-ref="HTTP_Listener_Configuration" path="apptest" doc:name="HTTP"/>        
<set-attachment attachmentName="test.json" value="{'Hi':'Hello'}" contentType="text/plain" doc:name="Attachment-JSON"/>
<set-attachment attachmentName="inputdata.json" value="{'k1':'v1','k2':'v2'}" contentType="text/plain" doc:name="Attachment"/>
<set-payload value="#['some data']" doc:name="Set Payload"/>
<http:request config-ref="HTTP_Request_Configuration" path="path2" method="POST" doc:name="HTTP"/>

Receiver:

<http:listener config-ref="HTTP_Listener_Configuration" path="path2" allowedMethods="POST" doc:name="HTTP"/>
<foreach collection="#[message.inboundAttachments]" doc:name="For Each">
    <set-payload value="#[payload.getInputStream()]" doc:name="Set Payload"/>
    <logger message="File Name: #[key]   Payload is: #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>           
</foreach>
AKB
  • 5,918
  • 10
  • 53
  • 90
0

I tested Postman sending binary data from a file upload and it seems like it sends the file contents in the request body, which means you will get that in the payload, not as inbound attachments. In order to have the file as an inbound attachment you should POST form-data with Postman so that a multipart request is sent, as mentioned by @Ralph. I've checked and that way it works. HTH.

afelisatti
  • 2,770
  • 1
  • 13
  • 21