In this post I'll explain you how to save a request body to a file using WSO2 ESB. Here the approach I'm using is File connector. I'm using ESB 4.9 and File connector 2.0.
Note the File connector version I'm using because the syntax/key words differ from connector version to version.
The message flow is depicted as shown below.
To achieve the above use case we can use two approaces.
1. Using VFS transport.
a. Fist you need to enable VFS transport in ESB. To do this follow this.
b. Now create the following API.
Note : In the above API I'm using a uri-template, If you use a uri-template the context will be appended to the end of the endpoint. So the ESB will try to access a location with the attached context which might cause issues. So to avoid this I'm using the following property to nullify the appending context.
2. You can achieve the same with file connector as well. Inorder to use file connector you need to install the file connector first. You can refer [1] for more details.
a. After installing the connector and enabling it you can create the following API to achieve the requirement.
Please drop a comment if you have any queries
[1] - https://store.wso2.com/store/assets/esbconnector/aec1554a-29ea-4dbb-b8c5-5d529a853aa2
Note the File connector version I'm using because the syntax/key words differ from connector version to version.
The message flow is depicted as shown below.
To achieve the above use case we can use two approaces.
1. Using VFS transport.
a. Fist you need to enable VFS transport in ESB. To do this follow this.
b. Now create the following API.
<api xmlns="http://ws.apache.org/ns/synapse" name="Api01" context="/api01"> <resource methods="POST" uri-template="/write-file-system"> <inSequence> <log level="full"> <property name="====== WRITING THE " value=" FILE ========"/> </log> <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" type="STRING"/> <property name="REST_URL_POSTFIX" value="" scope="axis2"/> <property name="OUT_ONLY" value="true" scope="default" type="STRING"/> <property name="transport.vfs.ReplyFileName" value="Hello55.txt" scope="transport" type="STRING"/> <log level="custom" separator=","> <property name="Output file" expression="$trp:transport.vfs.ReplyFileName"/> </log> <send> <endpoint> <address uri="vfs:ftp://ftpuser:ftpuser@localhost/files/in"/> </endpoint> </send> </inSequence> <outSequence/> <faultSequence/> </resource> </api>
Note : In the above API I'm using a uri-template, If you use a uri-template the context will be appended to the end of the endpoint. So the ESB will try to access a location with the attached context which might cause issues. So to avoid this I'm using the following property to nullify the appending context.
<property name="REST_URL_POSTFIX" value="" scope="axis2"/>
2. You can achieve the same with file connector as well. Inorder to use file connector you need to install the file connector first. You can refer [1] for more details.
a. After installing the connector and enabling it you can create the following API to achieve the requirement.
<api xmlns="http://ws.apache.org/ns/synapse" name="Api01" context="/api01"> <resource methods="POST" uri-template="/write-file-system"> <inSequence> <log level="full"> <property name="In The API ==============" value="==================="/> </log> <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2" type="STRING"/> <property name="OUT_ONLY" value="true" scope="default" type="STRING"/> <property name="transport.vfs.ReplyFileName" value="HelloConnector.txt" scope="transport" type="STRING"/> <property name="uri" value="ftp://ftpuser:ftpuser@192.168.48.119/files/in/" type="STRING"/> <log level="custom" separator=","> <property name="Output file" expression="$trp:transport.vfs.ReplyFileName"/> </log> <fileconnector.create> <source>{fn:concat($ctx:uri,$trp:transport.vfs.ReplyFileName)}</source> <inputContent>{$body}</inputContent> </fileconnector.create> </inSequence> <outSequence/> <faultSequence/> </resource> </api>
Please drop a comment if you have any queries
[1] - https://store.wso2.com/store/assets/esbconnector/aec1554a-29ea-4dbb-b8c5-5d529a853aa2