Getting Started
First, open Anypoint Studio and Create a New Mule Project.
Enter a project name and selection location and click finish.
Click on Add Modules in the Mule palette and find File in the featured components.
Drag and drop File onto the Mule palette before adding it to the project.
It should be like this after drag and drop.
Now let's add a new Mule Configuration File and give the name as global.
Enter the file name as global.
Go to Global Elements, click the Create button, and add an HTTP Listener config.
Leave everything as default and click ok.
Also, add a new File Config and click OK.
Select File Config and click OK.
Next, provide the Working Directory path in the General tab.
Next, drag and drop HTTP >> Listener from Mule palette to Message flow and give Display Name, select Connector configuration, and enter the Path.
Also, in Advanced properties, enter POST in Allowed methods.
Next, drag and drop the logger component from the Core palette to log the payload data.
Now let's create a JSON object which we will use in this example.
Save it as a JSON file on a desktop or any location.
We will use this file to create metadata type for all payload inputs.
Go back to HTTP listener and select Metadata in properties and click on the button Add metadata and click on the little icon to add new metadata.
Next, click on Add button and provide the type name "fileExample" and click Create type button.
Next, select metadata type, Select type ad JSON and select Example from the drop-down, and browse JSON file from the desktop and click Select.
So, this step will allow us to auto-complete our DataWeave expressions.
Next, go to Logger properties and click on Show graphical properties icon.
Click on Define metadata.
Select JSON object.
To test the object data, click on the Preview button.
Next, drag and drop the Set Variable component from the Core Mule palette.
Go to Metadata properties and select Json object.
Next, provide the Name of the variable in Settings and Value expression.
Set the DataWeave expression.
You can check the preview of data as well.
Finally, drag and drop the file write connector from the File module.
Next, enter the Path and dynamically define the name for the upcoming file by concatenating the variable to the file's name.
Next, set the content of the file.
Click on content, select Define metadata first, then define data weave expression.
After setting app properties in Connector configuration, Path, and Content, it will look like this.
Let's run the application.
Open Postman and enter Endpoint to see the output result.
Also, enter the Headers values and Body text.
Hit Send button.
As you see, the output is 200, which means api is running without any issues. Now go back to Endpoint Studio to check the log data.
Logger also logging the data correctly.
Now the final step is to test if the file has been created and data is written in the giver directory.
As you can see, a file has been created, and customer JSON data is also posted.
Conclusion
In this article, we learned to create and write CSV files using File Connector in MuleSoft and display results in Postman.