Reading Attachments in Mule

DZone 's Guide to

Reading Attachments in Mule

This article takes a look at reading attachments in Mule and also explores the configuration as well as the attachment metadata.

· Integration Zone ·
Free Resource
Let's say there is a requirement to handle a multipart request. As the request is received by Mule, the attachments are saved in an object called InboundAttchments, which is part of Mule Message.


  • Create a Mule flow with an HTTP listener.
  • First, to read the total attachments received. Drop a logger after HTTP and write #[message.inboundAttachments.size()] in the logger message.
  • To read the contents of attachment(s). Add a For Each. It will loop over the inbound Attachments object and read them one by one. Give #[message.inboundAttachments] against collection in properties tab.
  • Inside the For Each scope, place a logger to read the contents using #[message.payloadAs(java.lang.String)]. Reading the payload using  #[payload] will return an object.

Getting Attachment Metadata

  • Attachment Name To get the name of attachment, we can use the variable key, which is auto-generated by Mule inside the For Each scope#[flowVars.key] or #[payload.dataSource.part.fileName]
  • Attachment size (in Bytes) To get the attachment size, we have to read the payload and use the getBytes method #[message.payloadAs(java.lang.String).getBytes().length] or #[payload.dataSource.part.size]
  • Content Type To get the content type of the attachment: #[payload.dataSource.part.contentType]

Flow XML

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration"
host="" port="8081" doc:name="HTTP Listener Configuration" />
<flow name="attachmentFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/attachment" doc:name="HTTP" />
message="Total Attachments: #[message.inboundAttachments.size()]"
level="INFO" doc:name="Total Attachments" />
<foreach collection="#[message.inboundAttachments]" doc:name="For Each">
message="#["Attachment Name: " +flowVars.key+"\nAttachment Size (in Bytes): "+payload.dataSource.part.size +"\nContent-Type: "+payload.dataSource.part.contentType+"\nAttachmant Data: "+message.payloadAs(java.lang.String)]"
level="INFO" doc:name="Attachment Details" />

Thanks, and let me know your thoughts in the comments section!

mule 3.9 ,integration ,xml ,tutorial ,attachment metadata

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}