Anurag
Anurag

Reputation: 65

How to get information of attached file in mule?

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?

Upvotes: 1

Views: 4775

Answers (3)

AKB
AKB

Reputation: 5938

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>

Upvotes: 1

Ralph Rimorin
Ralph Rimorin

Reputation: 329

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; .. .. .. ..

Upvotes: 2

afelisatti
afelisatti

Reputation: 2835

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.

Upvotes: 0

Related Questions