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

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

Sponsored Content

Mule 4 MUnit multipart/form-data Payload

DZone's Guide to

Mule 4 MUnit multipart/form-data Payload

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

This article was provided by and does not represent the editorial content of DZone.

By 
Azam Shaikh user avatar
Azam Shaikh
DZone Core CORE ·
Jan. 29, 20 ·
Free Resource
Likes
Comment ( 0 )

Save
Tweet
Share
{{ articles[0].views | formatCount }} Views
  • Edit
  • Delete
  • Delete without notifying
  • {{ articles[0].isLocked ? 'Enable' : 'Disable' }} comments
  • {{ articles[0].isLimited ? 'Remove comment limits' : 'Enable moderated comments' }}

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

DOWNLOAD
Payload (computing)

Opinions expressed by DZone contributors are their own.

Partner Resources

Ă—

    {{ editionName }}

  • {{ node.blurb }}
    {{ node.type }}
    Trend Report

    {{ ::node.title }}

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}
by
DZone Core CORE
· {{ parent.articleDate | date:'MMM. dd, yyyy' }} {{ parent.linkDate | date:'MMM. dd, yyyy' }}
Tweet
{{ parent.views }} ViewsClicks