Handling File Attachments: handling multipart requests in Mule


Recently, I came across the following situation while working with Mule: I needed to handle an http post that would carry not one but N > 1 .

If I were to do this back in the days where I didn’t know about such a thing called “Mule”, I would have needed to:

  • Handle a http stream
  • Identify all the parts in the message
  • Read each file
  • Clean up

Of course there’re libraries and frameworks that can help you with this, but all of them still require some level of understanding of the multipart request beneath.

And then came Mule into my life, and this task became as simple as navigating the properties of a MuleMessage interface. Let’s explain a little bit….

As you probably know, what the MuleESB does is to carry packages of information (messages) from one system to another, allowing for software integration without the need of the involved systems to know about each other, their transport, protocols or any API changes. Each of those packages of information are represented by a MuleMessage. This object acts as a facade to access of a great deal of information about the message including, headers, payload and attachments, but today I want to focus on a property called inbound attachments.

When the message is coming through an http post that sends N >= 1 files through a Multipart Request, each of those files will be automatically read by Mule and stored in the message under the inboundAttachments property.

Pretty cool uh? No more worrying about Multipart, streams or anything like it, you just need to access this property as a Key-Value pair where the key is the filename and the value is the content itself. MuleMessage also provides a  getInboundAttachmentNames() that returns all the keys.

So, let’s see a couple of examples. Suppose you want to retrieve the content of an expected file named foo.txt:

Now, let’s suppose that you want a flow that receives N >= 1 amount of attachments and logs all of them:

That was easy wasn’t it? If you’re thinking that this is pretty simple stuff compared to the average post in this blog, then mission accomplished! We have succeeded in demonstrate how easy it is to do every day chores using Mule. Please let us know if this post makes your life easier, that would really make us happy.

We'd love to hear your opinion on this post

5 Responses to “Handling File Attachments: handling multipart requests in Mule”

  1. […] Handling File Attachments: handling multipart requests in Mule […]

  2. I am new to the mule. I am facing the same problem. help me

    Few details :

    Method in the connector :

    public Status addcontent(String inputXml, File file )
    throws IOException {
    // do something

    Entry in the connector.xml.sample

    Entry in the .mflow file

    I have a html file will will submit the data :


    function submitUploadForm(){
    var inputXml = ‘
    document.getElementById(“addcontentXml”).value = inputXml ;

    Upload Form

    When i hit the request i get the following exception on the mule server which i run in mule studio.

    INFO 2013-10-04 19:29:39,810 [[FusionliveTest].httpConnector.receiver.03] org.mule.module.xml.transformer.jaxb.JAXBContextResolver: No common Object of type ‘class javax.xml.bind.JAXBContext’ configured, creating a local one for: SimpleDataType{type=com.mclaren.fusionlive.common.Status, mimeType=’application/x-www-form-urlencoded’}, SimpleDataType{type=org.mule.api.transport.OutputHandler, mimeType=’*/*’}
    ERROR 2013-10-04 19:30:45,175 [[FusionliveTest].httpConnector.receiver.03] org.mule.exception.DefaultMessagingExceptionStrategy:
    Message : Could not find a transformer to transform “SimpleDataType{type=java.lang.String, mimeType=’*/*’}” to “SimpleDataType{type=java.io.File, mimeType=’*/*’}”.
    Code : MULE_ERROR-236
    Exception stack is:
    1. Could not find a transformer to transform “SimpleDataType{type=java.lang.String, mimeType=’*/*’}” to “SimpleDataType{type=java.io.File, mimeType=’*/*’}”. (org.mule.api.transformer.TransformerException)
    org.mule.registry.MuleRegistryHelper:252 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
    Root Exception stack trace:
    org.mule.api.transformer.TransformerException: Could not find a transformer to transform “SimpleDataType{type=java.lang.String, mimeType=’*/*’}” to “SimpleDataType{type=java.io.File, mimeType=’*/*’}”.
    at org.mule.registry.MuleRegistryHelper.lookupTransformer(MuleRegistryHelper.java:252)
    at org.mclaren.fusionlive.processors.AbstractExpressionEvaluator.transform(AbstractExpressionEvaluator.java:329)
    at org.mclaren.fusionlive.processors.AbstractExpressionEvaluator.evaluateAndTransform(AbstractExpressionEvaluator.java:202)
    + 3 more (set debug level logging or ‘-Dmule.verbose.exceptions=true’ for everything)

    Please put your suggestions if i am missing things..

    Thanks in advance.

  3. Hi Yogesh,
    I’ve answered in the forum ( http://forum.mulesoft.org/mulesoft/topics/http_file_upload_problem )

  4. I’ve been trying to use with this sample:


    But message.inboundAttachments is always empty, I even try with soapui and doesn’t work.

    • Hello John,

      Which version of Mule are you using? We spotted a bug around this in the 3.4.x series. Have you tried with 3.5.0?