DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

Trending

  • How an AI Software Factory Works: From Developer Intent to Production
  • GenAI Security Gateway: An End-to-End Architecture
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • Why Spark Executors Leave Cores Idle on YARN: A Packing Model for Executor Sizing

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Multiple Attachments in MuleSoft

Multiple Attachments in MuleSoft

In this post, a Mule-based developer shows us how we can handle attachments in MuleSoft, using the example of sending attachments to REST/SOAP API.

By 
Vikalp Bhalia
·
Mar. 24, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
16.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This article is all about how we can handle multiple attachments in MuleSoft.

Let's say we have a requirement to send attachments to a REST/SOAP API.

In order to achieve this one can follow the below steps:

1. Declare a variable which can store a list.    

<set-variable variableName="attachments" value="#[new.java.util.ArrayList()]" doc:name="attachments">

2. Use a foreach scope in order to handle multiple attachments and read attachments using the transform-message component. Add each attachment to the array list. 

<foreach collection="#[message.inboundAttachments]" doc:name="For Each">
<dw:transform-message doc:name="Read Attachment">
<dw:set-payload><![CDATA[
          %dw 1.0
          %output application/java
          ---
          {
          "fileName": payload.dataSource.part.fileName,
          "filedata": payload.inputStream,
          "fileSize": payload.dataSource.part.size
          }
        ]]></dw:set-payload>
    </dw:transform-message>
<expression-transformer expression="#[flowVars.attachments.add(payload)]" doc:name="Add attachment to List">
</foreach>

If you want to send an encoded string instead of a byte array, you can define a global function and use it as below:

<configuration doc:name="Configuration">
<expression-language>
<global-functions>
def byteToEncodedString(bytes) {
import org.apche.commons.codec.binary.Base64;
import java.lang.String;
byte[] encoded = Base64.encodeBase64(bytes);
String encoString = new String(encoded);
return encodedString;
}
</global-functions>
</expression-language>
</configuration>

Use the above global function in DataWeave:

          %dw 1.0
          %output application/java
          ---
          {
          "fileName": payload.dataSource.part.fileName,
          "filedata": byteToEncodedString(payload.inputStream),
          "fileSize": payload.dataSource.part.size
          }

Set payload with value = flowVars.attachments.

Now if you have any specific request format for an API, you can easily transform the current payload in the required format using a Transform Message component.

MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook