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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

Trending

  • How GitHub Copilot Helps You Write More Secure Code
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • Web Crawling for RAG With Crawl4AI
  • MySQL Formatter: How to Make Beautiful Code and Why You Need 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 user avatar
Vikalp Bhalia
DZone Core CORE ·
Mar. 24, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
15.7K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide
  • Generic and Dynamic API: MuleSoft

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!