Multipart Form-Data Handling in MuleSoft
Overview: –
- Multipart form-data is a crucial feature for APIs that handle file uploads or complex form submissions. MuleSoft provides comprehensive support for multipart form-data, making it easy to design, process, and integrate with APIs that require this functionality.
- In modern web applications, user interactions often require complex data submissions, such as uploading files alongside form fields.
- Multipart form-data makes these scenarios seamless by bundling all content in a single request and ensures interoperability across platforms, browsers, and tools that support HTTP.
- In this blog, we’ll explore: What multipart form-data is and its use cases. How to work with multipart form-data in MuleSoft.
What is Multipart Form-Data?: –
- Multipart form-data is a MIME type that allows sending multiple parts of data in a single HTTP request.
- It is designed to handle complex data submissions where multiple types of content are combined in a single request. Unlike simple form submissions (application/x-www-form-urlencoded), which encode data as a URL-encoded query string, multipart form-data supports structured submissions, including files, text fields, and binary data.
Key Characteristics of Multipart Form-Data:-
- Multiple Content Parts:
Each submission can have multiple parts, with each part containing its own content and metadata. This allows combining different types of data, such as files, text, and JSON objects, in a single request. - Boundary Separator:
A unique boundary string is used to delimit the parts in the request. This boundary ensures that the receiving system can separate and process each part independently. - Flexible Content-Type:
Each part of the request can have its own Content-Type header, allowing for diverse content formats within the same submission (e.g., text/plain, application/json, image/png, etc.). - Metadata for Each Part:
Metadata headers, such as Content-Disposition, specify the name of the form field, filename (if applicable), and other attributes to describe the content.
Example: –
- It is commonly used for:
-
- File Uploads: You need to send one or multiple files (e.g., images, documents, or videos) to a server.
- Mixed Content: A single request contains text fields, JSON, or other structured data along with files.
- API Interactions: Many APIs require multipart form-data for operations such as uploading media files to cloud storage.
-
Multipart Form-Data in MuleSoft: –
- MuleSoft simplifies the handling of multipart form-data through its HTTP connectors and DataWeave capabilities. It parses incoming multipart requests into a structured payload, allowing easy access to individual parts.
- When you convert Multipart Form-Data to JSON using Dataweave it will look like this:-
- To understand the concept of multipart form-data more clearly, let’s create an application that accepts multipart data with two parts:
- Name (text) – A simple string input.
- employee.json (file) – A JSON file containing employee details.
The application extracts the provided name, uses it as the filename, and creates a new JSON file containing the contents of the employee.json file.
This practical example will demonstrate how to process both text and file data in a single HTTP request.
Use case: –
1.Create a new project in Anypoint Studio. To create click on the “File” menu at the top left corner of the window. From the dropdown menu, select “New” and then choose “Mule Project”.
2.A dialog box will appear where you can enter details about your new Mule project. Provide a name for your project in the “Project name” field. And click on finish.
3.Drag and drop the HTTP Listener Connector into the project from the HTTP module. Configure the listener, and set the host to All interface (0.0.0.0), port number as 8081 by clicking on the (+) in Connector configuration. Set the path as /demo.
4.Drag and drop the logger from the core module and configure the message as #[payload] to log the input payload.
5.Drag and drop the transform message component to the canvas and write a DataWeave code to convert data to JSON and again add the logger to log converted payload
6.Drag and drop the write component from the file module and capture the parts using DataWeave script, provide the file path by combining the base directory, the file name (payload.parts[0].content), and the file extension(.csv) and given content as payload.parts[1].content
Eg: path: “D:\\New folder\\demo\\” ++ payload.parts[0].content ++ “.csv”
Note: – We can access and transform multipart data using parts (by index or name), retrieve the main data with ‘content’, and access metadata with ‘headers’.
- If you know the key name we can access content in dataweave using #[payload.parts.fileName.content]
- If you know the index we can access content in DataWeave using #[payload.parts[0].content] (In this use case, I am capturing data using an index)
7.Add a transform message from core components and convert the data as shown in below image and then add a logger to log the data.
8.Deploy the application, to deploy the application we have to right click on the project, go to Run As-> Mule Application
9.After the application is deployed, Open Postman and provide the URL as http://localhost:8081/demo , Select body as form-data
10.For 1st part Add keyName, select type as text, and provide a filename
For 2nd part Add keyName, select type as file, and Select a CSV file from the local directory and Send a request
Now you can check that a new file has been created in the given directory
Here, We have successfully created an application to receive Multi-part form data lets create another application to consume this application and learn how to How to send payload as a multipart/form File in a HTTP Request
2. How to send payload as a multipart/form-data in an HTTP Request
- Create an application in Anypoint Studio like before and add HTTP Listener Connector into the project from the HTTP module and change the port as 8083 and path as ‘consume’
2.Drag and drop the read component from the file module and add a path to read a CSV file By clicking on ‘…’ and add a logger to log the payload.
3.Add the “Transform Message” component from the Core module and configure it to format the data for the HTTP request, setting the request body to multipart/form-data as shown below. And add a logger to log the payload.
%dw 2.0
output multipart/form-data boundary=’—-34b21′
—
{
“parts”: {
“fileName”: {
“headers”: {
“Content-Disposition”: {
“name”: “fileName”,
“subtype”: “form-data”
}
},
“content”: “EmployeeRecords”
},
“employees”: {
“headers”: {
“Content-Disposition”: {
“name”: “employees”,
“filename”: “Employees.csv”,
“subtype”: “form-data”
},
“Content-Type”: “text/csv”
},
“content”: payload
}
}
}
For boundary you can use anyone you prefer, in this case boundary is set to “————————–abc123”
4.Drag and drop the HTTP request from the HTTP module and click “+” to add a configuration and provide the host as localhost, port as 8081, then click ‘ok’ and In the component provide method as POST and path as ‘demo’ and add a logger to log the payload
5. Drag and drop the transform message component to the canvas and write a DataWeave code to convert data to JSON and again add the logger to log converted payload and save all.
6.To deploy both applications, we have to right-click on the project, go to Run As-> Mule Application (Configuration)
7.Select both applications and click on Run to deploy the application.
8.Once both the applications are deployed, Open Postman and provide the URL as http://localhost:8083/consume