You can design, integrate, and manage APIs for a variety of services and applications using MuleSoft, a robust platform. One of the most frequent problems developers encounter is how to save and retrieve any kind of material—pictures, movies, documents, and so on—without sacrificing the integrity or quality of the item.
This blog post will show you how to create a MuleSoft API that uses a MySQL server database to store and retrieve any kind of file. Postman will be used for testing.
Prerequisites
To follow along with, you will need:
- Anypoint Studio 7.9 or later
- MySQL Server account( Register (freesqldatabase.com) )
- Postman
Step 1: Create a MySQL account
To create an account, click MySQL Account Registration. Access the account by logging in. Choose the location of the server. Select Database Details, then click Start to generate the login credentials. Hold off till the database becomes active. Utilizing the script in the Appendix section, create a fileInfo table.
Step 2: Create a Mule Project
To begin with, we must open Anypoint Studio and create a new Mule project. To begin, select File > New > Mule Project and provide a name, like “file-storage-api.” Click Finish after choosing the Mule runtime version of 4.3.0 or later.
Step 3: Add Dependencies
The next step is to add dependencies to our project. We will utilize these connectors and modules to put our API logic into practice. To accomplish this, add the following dependencies to your project’s pom.xml file:
<dependencies> <dependency> <groupId>org.mule.connectors</groupId> <artifactId>mule-http-connector</artifactId> <version>1.6.0</version> <classifier>mule-plugin</classifier> </dependency> <dependency> <groupId>org.mule.connectors</groupId> <artifactId>mule-sockets-connector</artifactId> <version>1.2.2</version> <classifier>mule-plugin</classifier> </dependency> <dependency> <groupId>org.mule.connectors</groupId> <artifactId>mule-file-connector</artifactId> <version>1.3.4</version> <classifier>mule-plugin</classifier> </dependency> <dependency> <groupId>org.mule.connectors</groupId> <artifactId>mule-db-connector</artifactId> <version>1.11.0</version> <classifier>mule-plugin</classifier> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency></dependencies>Wait for the dependencies to download after saving the file.
Step 4: Configure the MySQL DatabaseConnector
The database connector that enables communication with MySQL must be configured before we can begin implementing the API logic. Click Create and choose Database_Config: MySQL Connection from the Global Elements tab after opening the fs-db-filestore-retrive.xml file.
Enter the information below in the configuration window:
- Host: sql12.freesqldatabase.com
- Port: 3306
- User: YourUserName
- Password: YourPassword
- Database: YourDatabaseName
Click OK to save the configuration.
Step 5: Implement the API Logic
We can now put the API logic into practice. To achieve this, select the Message Flow tab and take the following actions:
POST /postData
By uploading a file to MySQL, this API will return the file’s metadata in JSON.
- From the Mule Palette, drag and drop an HTTP Listener component onto the canvas. An HTTP Listener configuration will be created as a result. Put in the following information:
- Name: httpListenerConfig
- Host: 0.0.0.0
- Port: 8081
Click OK to save the configuration.
2. Set the Path to /postData by double-clicking on the HTTP Listener component.
3. Drop a database component into the POST:/postData flow, following the Transform Message component, by dragging it from the Mule Palette into the canvas. To adjust its properties, double-click on it and select:
- Set connector configuration
- SQL query test
- Input params

Image notes:
GET /getData
When a file is uploaded to MySQL, this endpoint returns the JSON information for that file.
- From the Mule Palette, drag and drop an HTTP Listener component onto the canvas. An HTTP Listener configuration will be created as a result. Put in the following information:
- Name: httpListenerConfig
- Host: 0.0.0.0
- Port: 8081
Click OK to save the configuration.
2. After the Transform Message component, drag and drop a database component from the Mule Palette onto the canvas to place it in the POST:/postData flow. Set the following properties by giving it a double-click:
- Set connector configuration
- SQL query test
- Input params
3. In order to write the file in a certain spot, drag the file connector. Set the following properties by giving it a double-click:
- Set connector configuration
- Path
- Content

Image notes:

Image notes:
Step 6: Test the API
We can now use Postman or any other API testing tool to test our API. Take these actions to accomplish this:
- Open Postman and create a new request.
- Put http://localhost:8081/postData as the URL and select POST as the method.
- Add a key-value pair with filePath as the key and whatever file you want as the value in the params tab.
- When you receive a 201 response with a JSON object, click Send again.
- Enter http://localhost:8081/getData in a new GET request that you create.
- When you receive a 200 response with a JSON array containing all of the metadata for your file, click Send.
- Once the data has been retrieved, you may confirm that the file is corrupt-free and that it is kept in the designated path.
Appendix
SQL Query to create the table:
create table sql12629982.fileInfo(filename varchar(255), filetype varchar(20),content LONGBLOB,lastupdate TIMESTAMP default now());
Project GIT URL : <u><em>Store-and-Retrieve-Any-Type-of-File-with-MuleSoft-API</em></u>
Conclusion
I hope you have learned something new and appreciated this post. You may successfully construct a MuleSoft API that uses databases to store and retrieve any kind of file by following these instructions. Please feel free to comment below with any queries or suggestions. I am appreciative of your reading!
]]>