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

  • Why 200 OK Is the Most Dangerous Lie in Enterprise Integration
  • Streaming vs In-Memory DataWeave: Designing for 1M+ Records Without Crashing
  • Build a Dynamic Web Form Using Camunda BPMN and DMN
  • Automating a Web Form With Playwright MCP and MySQL MCP

Trending

  • Testing Asynchronous Systems, Part 1: Mapping the System and Classifying Its Asynchrony
  • Spring AI on Local LLMs
  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • Choosing a BBD Framework

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. File Attachment Handling in Mule 4 (Use of multipart/form-data)

File Attachment Handling in Mule 4 (Use of multipart/form-data)

The following illustration explains how an HTTP POST request can be configured for multiple files in a single request using RAML 1.0 and Mule version 4.1.4.

By 
Jagruti Sakhare
·
Dec. 26, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
51.4K 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

The following illustration explains how an HTTP POST request can be configured for multiple files in a single request using RAML 1.0 and Mule version 4.1.4.

RAML 1.0 construct:

Image title

It has the 'type' abstarction for multiple file upload. And inside the the POST body, use it as a array along with multipart/form-data . 'minItems' represent the number of minimum items in the array. This can be used to make the field mandatory.

Now , to send the file in HTTP request from another Mule application the payload has to be set with certain parameters. Multipart content can be generated usnig Dataweave by building an object with list of parts (multiple file parts)each containing its header and content. The parts can have data other than file, e.g 'Contet-Type= text/plain'

Set Payload For Multipart Input

The example mentioned above is only one "part" configured in it. Several different data parts can be added in a single payload. Headers will have its metadata. Key and Filename will be added under "Content-Disposition." Content will have actual file content, and it can be read from payload or can be given directly. e.g:

jsonfile : {
      headers : {
        "Content-Disposition" : {
            "name": "jsonfile",
            "filename": "sample.json"
        },
        "Content-Type" : "application/json"
      },
      content : {
        Name: "John Doe",
        ID: "1234",
        Number: 01
      }
    }

Dataweave script can be sued to access the data coming in multipart. We have set the part key to "FILE-ATTACH" in above example. We can retrieve contents as:

%dw 2.0
output application/java
input payload multipart/form-data
---
payload.parts.'FILE-ATTACH'.content

Similarly, headers can be read as:

%dw 2.0
output application/java
input payload multipart/form-data
---
payload.parts.'FILE-ATTACH'.headers.'Content-Disposition'.filename

If in the flow the payload is not coming from the file itself and it is generated by DataWeave mapping or any other connector, then the "mime-type" of the payload has to be set to "application/octet-stream." Then it can be used while setting multi parts payload. This is because, after DataWeave, the mime type is set differently than required in the multi-part.

MULE Form (document)

Opinions expressed by DZone contributors are their own.

Related

  • Why 200 OK Is the Most Dangerous Lie in Enterprise Integration
  • Streaming vs In-Memory DataWeave: Designing for 1M+ Records Without Crashing
  • Build a Dynamic Web Form Using Camunda BPMN and DMN
  • Automating a Web Form With Playwright MCP and MySQL MCP

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