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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • MDC Logging With MuleSoft Runtime 4.4
  • HTTP Deep-Dive With Ballerina: Client Communication
  • Convert Payload From XML to Array of Objects by Comparing Mule 3 and Mule 4 Written Data Weave
  • Mule 4: JMS Pub and Sub With Transformation

Trending

  • Top 7 Best Practices DevSecOps Team Must Implement in the CI/CD Process
  • Writing Reusable SQL Queries for Your Application With DbVisualizer Scripts
  • Auto-Scaling DynamoDB Streams Applications on Kubernetes
  • Navigating the Skies
  1. DZone
  2. Coding
  3. Languages
  4. Reading Attachments in Mule

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.

Abhay Yadav user avatar by
Abhay Yadav
·
Updated Sep. 06, 18 · Tutorial
Like (3)
Save
Tweet
Share
14.96K Views

Join the DZone community and get the full member experience.

Join For Free
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.

Configuration

  • 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:doc="http://www.mulesoft.org/schema/mule/documentation"
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="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" />
<flow name="attachmentFlow">
<http:listener config-ref="HTTP_Listener_Configuration"
path="/attachment" doc:name="HTTP" />
<logger
message="Total Attachments: #[message.inboundAttachments.size()]"
level="INFO" doc:name="Total Attachments" />
<foreach collection="#[message.inboundAttachments]" doc:name="For Each">
<logger
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" />
</foreach>
</flow>
</mule>

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

Object (computer science) Requests Payload (computing) Flow (web browser) Metadata Requirement Drops (app) XML

Opinions expressed by DZone contributors are their own.

Related

  • MDC Logging With MuleSoft Runtime 4.4
  • HTTP Deep-Dive With Ballerina: Client Communication
  • Convert Payload From XML to Array of Objects by Comparing Mule 3 and Mule 4 Written Data Weave
  • Mule 4: JMS Pub and Sub With Transformation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: