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
Refcards
Trend Reports

Events

View Events Video Library

Related

  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

Trending

  • Token Attribution Framework for Agentic AI in CI/CD
  • Pragmatica Aether: Let Java Be Java
  • Detecting Plan Regression in SQL Server Using Query Store
  • The Agentic Agile Office: Streamlining Enterprise Agile With Autonomous AI Agents

Mule 4 MUnit multipart/form-data Payload

The following article gives an example of posting multipart/form-data payload in MUnit.

By 
Azam Shaikh user avatar
Azam Shaikh
·
Jan. 29, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
26.1K Views

Join the DZone community and get the full member experience.

Join For Free

The following article gives an example of posting a multipart/form-data payload in MUnit. 

  • Mule version: 4.2.2
  • MUnit version: 2.2.2

This article assumes that you have already built a RAML and have a POST method accepting multipart/form-data content and asserting it can be done as per your requirements.

The POST method in this article expects the following:

  • JSON - JSON file as a part
  • Multiple binary files as part

When the request is made to this POST method from Postman or ARC, the content type is selected as multipart/form-data, and files are selected using the select/choose file button. But to make the same request in MUnit where the mock request needs to be sent to the POST method, use the following example.

You might also want to read:  File Attachment Handling in Mule 4 (Use of multipart/form-data)

Under src/test/resources in your application, create folder files/request.

Under files/request folder, copy the mock request files for multipart.


From the src/test/munit folder, open the test suite file and drop Set Event under Behavior from Palette -> MUnit.

Go to Set Event properties under the General tab and update the payload Media type to multipart/form-data and the value.

Note: Remember to include #[ ] to make it an expression since the button is not available here.

Here is the complete payload value with #[ ]

JSON
x
57
 
1
#[
2
%dw 2.0
3
output multipart/form-data
4
---
5
{
6
    parts: {
7
        json: {
8
            headers: {
9
              "Content-Disposition": {
10
                name: "json",
11
                subtype: "form-data",
12
                filename: "postReq.json"
13
              },
14
              "Content-Type": "application/json",
15
              "Content-Transfer-Encoding": "binary"
16
            },
17
            content: MunitTools::getResourceAsStream('files/request/postReq.json')
18
        },
19
        one: {
20
            headers: {
21
              "Content-Disposition": {
22
                name: "one",
23
                subtype: "form-data",
24
                filename: "1.pdf"
25
              },
26
              "Content-Type": "application/pdf",
27
              "Content-Transfer-Encoding": "binary"
28
            },
29
            content: MunitTools::getResourceAsStream('files/request/1.pdf')
30
        },
31
        two: {
32
            headers: {
33
              "Content-Disposition": {
34
                name: "two",
35
                subtype: "form-data",
36
                filename: "2.pdf"
37
              },
38
              "Content-Type": "application/pdf",
39
              "Content-Transfer-Encoding": "binary"
40
            },
41
            content: MunitTools::getResourceAsStream('files/request/2.pdf')
42
        },
43
        three: {
44
            headers: {
45
              "Content-Disposition": {
46
                name: "three",
47
                subtype: "form-data",
48
                filename: "3.pdf"
49
              },
50
              "Content-Type": "application/pdf",
51
              "Content-Transfer-Encoding": "binary"
52
            },
53
            content: MunitTools::getResourceAsStream('files/request/3.pdf')
54
        }
55
    }
56
}
57
]


Thanks for reading!

Further Reading

Testing REST API File Uploads in JMeter

Transforming multipart/form-data Into XML Using MuleSoft and JavaScript

Payload (computing)

Opinions expressed by DZone contributors are their own.

Related

  • When One Giant Payload Must Serve Many Small Consumers: Designing a Scalable Fanout Service
  • How to Enhance the Performance of .NET Core Applications for Large Responses
  • Automatic 1111: Custom Sketch-to-Image API
  • On-Demand-Schedulers With MuleSoft CloudHub APIs

Partner Resources

×

Comments

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

  • 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