Reading Time: 5 minutes

I was recently working on a project where we had to handle SOAP attachments. Working with SOAP attachments is the kind of thing that you work on every 3-5 years and then 10 seconds after you are done you forget all about them. All the information required is available in our docs but it can still be good to have a complete end to end example as a reference. Esteban Robles Luna's (former MuleSoft colleague) blogpost, Working with SOAP attachments using Mule's CXF module, from 2011 was most helpful.

When working on this project I also wanted to see if how this could be implemented using RAML and REST services instead of SOAP. When researching the topic it seems like there is no complete consensus for how to do this, but I found this discussion – How do I upload a file with metadata using a REST web service? – quite interesting.

latest report
Learn why we are the Leaders in management and iPaaS

The use case is very straightforward, sending and receiving a PDF file as a SOAP attachment and as a REST attachment. The application has four different flows:

  • Read a PDF file from disc and then add it as a SOAP attachment to the request.
  • Expose a SOAP service that is capable of receiving a SOAP request with an attachment.
  • Read a PDF file from disc and add that as an attachment to a REST call.
  • Expose a REST service using APIkit and RAML that is able to handle a request with an attachment.

1. SOAP Attachment – Client

This is probably the most complicated flow since it requires some 4 lines of code in order to create the attachment and uses the CXF module to configure the client piece. Trigger the flow by copying the file src/test/resources/esb.pdf to the folder src/test/resources/soap/attachment/in. The configuration file can be found here:


2. SOAP Attachment – Server

This flow exposes a SOAP web service and retrieves the SOAP attachment from the request and writes that to disk. The service is triggered when triggering the client, if you want to test it it individually just use SOAP UI and point it to the endpoint (http://localhost:8883/contacts). The configuration file for this web service can be found here:


3. REST Attachment – Client

This implementation is very straightforward, just use the attachment message processor and the outbound HTTP call to make the call. You can trigger the flow by copying the file src/test/resources/esb.pdf to the folder src/test/resources/rest/attachment/in. The configuration file for this can be found here:


4. REST Attachment – Server

The REST service accepting is autogenerated using a RAML file that can be found here. The API has some additional methods not used in this example. The example can be triggered by the client described in step 3 by copying the file to the right folder or by any REST console that can take an attachement by posting to this URL: http://localhost:8884/api/contact/abc/datasheet. The configuration for this API can be found here:

The complete project is available in github: