WSO2 ESB HTTP POST with form data - wso2

I have a soap service that I want to turn around and post a message to an external server.
I was able to do this via curl like so:
curl --data-urlencode "filename=data.txt" --data-urlencode "filedir=/home/myfile/in"
--data-urlencode "busproc=MyBP" --data-urlencode "serverip=192.168.1.4"
--data-urlencode"uid=myuserid" --data-urlencode "pwd=mypwd"
http://somelocation.com:8833/webservice/inbound/here
But I can't quite get it working correctly. Here's my proxy service:
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="ExampleHTTPPostWithFormData"
transports="http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log/>
<property name="messageType"
value="application/x-www-form-urlencoded"
scope="axis2"
type="STRING"/>
<property name="HTTP_METHOD" value="post" scope="axis2" type="STRING"/>
<send>
<endpoint>
<address uri="http://somelocation.com:8833/webservice/inbound/here"
format="pox"/>
<property name="uid" value="user"/>
<property name="pwd" value="password"/>
<property name="filedir" value="/home/myfile/in"/>
<property name="busproc" value="myBP"/>
<property name="serverip" value="192.168.1.4"/>
<property name="filename" value="data.txt"/>
</endpoint>
</send>
<log level="full"/>
</inSequence>
</target>
<description/>
</proxy>
The end service seems to only see me posting to the URL (but not the passed in data properties).

Properties are not the way to build the message content. The best way I've found to do it is with a payloadFactory. The message you need to build has a root XML element with one child per form field, and then it seems that Axis2 handles the messageType of application/x-www-form-urlencoded by serialising in the appropriate format. So a minimal change to your proxy would be:
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="ExampleHTTPPostWithFormData"
transports="http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<log/>
<property name="messageType"
value="application/x-www-form-urlencoded"
scope="axis2"
type="STRING"/>
<payloadFactory media-type="xml">
<format>
<params xmlns="">
<uid>user</uid>
<pwd>password</pwd>
<filedir>/home/myfile/in</filedir>
<busproc>myBP</busproc>
<serverip>192.168.1.4</serverip>
<filename>data.txt</filename>
</params>
</format>
</payloadFactory>
<send>
<endpoint>
<address uri="http://somelocation.com:8833/webservice/inbound/here"
format="rest"/>
</endpoint>
</send>
<log level="full"/>
</inSequence>
</target>
<description/>
</proxy>
It may also be convenient to add <property name="FORCE_HTTP_1.0" value="true" scope="axis2" type="STRING"/> depending on whether your REST service handles HTTP/1.1.
If you need parameters then you can pass in arguments to the payloadFactory, using the XPath extensions. E.g.
<payloadFactory media-type="xml">
<format>
<params xmlns="">
<uid>user</uid>
<pwd>password</pwd>
<filedir>/home/myfile/in</filedir>
<busproc>myBP</busproc>
<serverip>192.168.1.4</serverip>
<filename>$1</filename>
</params>
</format>
<args>
<arg evaluator="xml" expression="$ctx:filename"/>
</args>
</payloadFactory>

If your sending the SOAP payload in a file you would need to use the VFS transport. Please refer to the following sample on how to use the VFS transport to solve your issue
http://docs.wso2.org/pages/viewpage.action?pageId=26838852
Alternatively you can use SOAPUI or any SOAP client to send the payload directly to the ESB proxy endpoint

Related

WSO2 Ei 6.1 email received as attachment

I have a service which send the email from WSo2 server. But i am getting the message content as attachment and not in body of the email.
You can define the message content for the email body using payload factory mediator as below.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="mailtest"
startOnLoad="true"
statistics="disable"
trace="disable"
transports="http,https">
<target>
<inSequence>
<send>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<payloadFactory media-type="xml">
<format>
<test xmlns="">$1</test>
</format>
<args>
<arg xmlns:ax21="http://services.samples/xsd"
xmlns:ns="http://services.samples"
evaluator="xml"
expression="//ns:getQuoteResponse/ns:return/ax21:name"/>
</args>
</payloadFactory>
<property name="Subject" scope="transport" value="Sample Mail"/>
<property name="OUT_ONLY" scope="default" type="STRING" value="true"/>
<property name="messageType"
scope="axis2"
type="STRING"
value="text/plain"/>
<property name="OUT_ONLY" value="true"/>
<send>
<endpoint>
<address uri="mailto:yourmail#gmail.com"/>
</endpoint>
</send>
</outSequence>
</target>
<description/>
</proxy>
If you want to define a message body with the email attachment, use "transport.mail.bodyWhenAttached" property.

Creating a file using Wso2Esb getting stuck

Hi am working with Wso2Esb 4.7.0
I have created a proxy which creates a file in the local system, Below is the proxy and its sequence, I have uncommented the required vfs transport in axis2 file.
Proxy service:
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="FileProxy"
transports="https http"
startOnLoad="true"
trace="enable">
<description/>
<target>
<inSequence>
<property name="messageType" value="application/json" scope="axis2"/>
<property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
<property name="Body"
expression="$body"
scope="default"
type="STRING"/>
<property name="transport.vfs.ReplyFileName"
expression="fn:concat(fn:substring-after(get-property('MessageID'), 'urn:uuid:'), '.txt')"
scope="transport"/>
<payloadFactory>
<format>
<Reading>$1</Reading>
</format>
<args>
<arg evaluator="xml" expression="get-property('Body')"/>
</args>
</payloadFactory>
<property name="OUT_ONLY" value="true"/>
<property name="messageType"
value="application/json"
scope="axis2"
type="STRING"/>
<send receive="fileWriteSequence">
<endpoint>
<address uri="vfs:file:///home/youtility/Documents/Capp_services/wso2esb-4.7.0/Files"/>
</endpoint>
</send>
</inSequence>
<outSequence onError="fault">
<send/>
</outSequence>
</target>
</proxy>
Sequence:
<sequence xmlns="http://ws.apache.org/ns/synapse" name="fileWriteSequence">
<property name="messageType" value="application/json" scope="axis2"/>
<property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
<log level="custom">
<property name="sequence" value="fileWriteSequence"/>
</log>
<payloadFactory media-type="xml">
<format>
<ResponseJSON xmlns="">
<Body>
<Datalist>file Created</Datalist>
</Body>
<Status>200</Status>
</ResponseJSON>
</format>
<args/>
</payloadFactory>
<send/>
</sequence>
curl:
curl -v -H "Accept: application/json" -H "Content-Type:application/json" -d '{"test1":"1","test2":"-1","test3":"-1"}' http://localhost:8282/services/FileProxy
When am trying to call this service with the curl file is getting created in the mentioned file location with the given content but am not getting any response as file Created as mentioned in the sequence, Actually after creating the file the process is not going the the fileWriteSequence so it is getting stuck.
Any Suggestions..
You are in OUT_ONLY, (you have set <property name="OUT_ONLY" value="true"/>) and you must be in OUT_ONLY when you create a file because there is no response from the filesystem
Therefore, the "receive" sequence will never be executed : add it's content after the send mediator

WSO2 ESB Content is not allowed in prolog exception in a OutSecuence

I am developing a proxy service with a soap webservice that performs soap to rest conversion, the message is sent to a servlet that response with a string in flat format (not xml), just a secuence of characters like
OIUW|ECHNOWE|RFHQWIUE|FBPQW|EFHAO|IEFH
I am invoking with SOAP UI and I get this response fine, now I would like to receive it in "SOAP format", wrapping the message into a soap:body, I've tried with a XSLT and with a PayloadFactory Mediator, but as soon as I use any of them (even doing nothing) I get a
[2014-07-31 09:30:41,847] ERROR - RelayUtils Error while building Passthrough stream
org.apache.axiom.om.OMException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
What do I do wrong ? How can I achieve a message transformation without this exception?
Thank you!
UPDATE: My proxy as requested by Ratha
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="SCL3"
transports="http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<log level="custom">
<property name="MyTrace" value="--- REQUEST ---"/>
</log>
<log level="full"/>
<property name="REST_URL_POSTFIX"
value="x4?msg=x4|0003|0000000021|0|0|0400002081020224849"
scope="axis2"
type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<property name="SOAPAction" scope="default" action="remove"/>
<header name="Action" scope="default" action="remove"/>
<send>
<endpoint>
<address uri="http://localhost:8087/X4" format="pox"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="custom">
<property name="MyTrace" value="--- RESPONSE ---"/>
</log>
<property name="ContentType"
value="application/soap+xml"
scope="transport"
type="STRING"/>
<property name="messageType"
value="application/soap+xml"
scope="transport"
type="STRING"/>
<payloadFactory media-type="xml">
<format>
<a xmlns="">$1</a>
</format>
<args>
<arg value="my value"/>
</args>
</payloadFactory>
<send/>
</outSequence>
</target>
<publishWSDL uri="file:/C:/wso2/wso2esb-4.8.1/repository/workspaces/myproject/SCL3.wsdl"/>
</proxy>
I've seen that my servlet was setting content type to "text/xml" instead of "text/plain", I've changed it to "text/plain" and everything is working fine now.
Therefore I deduce that the error message
"Content is not allowed in prolog"
actually means
"Unexpected content type"
When you have following outsequence configuration what your log prints?
<outSequence>
<log level="full">
<property name="MyTrace" value="--- RESPONSE ---"/>
</log>
<send/>
</outSequence>

Is it possible with WSO2ESB to send E-Mail Using Header

i wish to send a e-mail to my client using Header mediator .
Is it possible with that or not
My proxy like this
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="Mail"
transports="https http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<log/>
<property name="workpartybranchid"
expression="//workpartybranchid/text()"
scope="default"
type="STRING"/>
<payloadFactory>
<format>
<p:SelectMail_Op xmlns:p="http://ws.wso2.org/dataservice">
<p:workpartybranchid>$1</p:workpartybranchid>
</p:SelectMail_Op>
</format>
<args>
<arg expression="get-property('workpartybranchid')"/>
</args>
</payloadFactory>
<send receive="Mailseq">
<endpoint>
<address uri="http://192.168.1.4:9773/services/muser_DataService/"
format="soap11"/>
</endpoint>
</send>
</inSequence>
</target>
</proxy
and corresponding sequence is
<sequence xmlns="http://ws.apache.org/ns/synapse" name="Mailseq" onError="fault">
<property name="CONTENT_TYPE"
value="application/json"
scope="axis2"
type="STRING"/>
<property name="FORCE_ERROR_ON_SOAP_FAULT" value="true"/>
<property xmlns:ns="http://org.apache.synapse/xsd"
xmlns:s="http://ws.wso2.org/dataservice"
name="primarymail"
expression="//s:primarymail/text()"
scope="axis2"
type="STRING"/>
<log>
<property xmlns:ns="http://org.apache.synapse/xsd"
xmlns:s="http://ws.wso2.org/dataservice"
name="primarymail"
expression="//s:primarymail/text()"
scope="axis2"
type="STRING"/>
</log>
<header name="To" expression="fn:concat('mailto:', get-property('primarymail'))"/>
<send/>
<log>
<property name="mail" value="ts working"/></log>
</sequence>
can we send a email like this i have got reference from this
http://docs.wso2.org/wiki/display/ESB460/Sample+256%3A+Proxy+Services+with+the+Mail+Transport
i am posted my requirement in another way also please refer this
Can we send Multiple Mail in wso2esb in same sequence or dynamically set a mail
Just follow the step in below URL ( but rather than header part, use endpoint for this as below.. this worked for me)
http://docs.wso2.org/wiki/display/ESB460/Sample+256%3A+Proxy+Services+with+the+Mail+Transport
Add Send for mail as below...( no need header mediator)
<send> <endpoint>
<address uri="mailto:yourmailaddr#gmail.com" />
</endpoint>
</send>

Can we append the data in single text file using WSO2ESB?

I am getting error message from mobile app or somewhere any of front end.
I need keep that message in one single text file. That file could be stored in my local system.
How can I do this using WSO2ESB?
<proxy xmlns="http://ws.apache.org/ns/synapse" name="AppendFile" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence onError="fault">
<property name="error_TYPE" expression="//error/text()" scope="axis2" type="STRING"/>
<property name="HTTP_METHOD" value="POST" scope="axis2" type="STRING"/>
<payloadFactory>
<format>
<error></error>
</format>
<args>
<arg expression="get-property('error')"/>
</args>
</payloadFactory>
<send>
<endpoint>
<address uri="//home/desktop/append.txt"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="CONTENT_TYPE" value="application/json" scope="axis2"/>
<log level="full"/>
<send/>
</outSequence>
</target>
<description></description>
</proxy>
But its giving transport error.
How can we do this? Please suggest.
You can use VFS transport. Please refer.
http://achala11.blogspot.com/2012/08/vfs-transport-wso2-esb-using-file.html
http://docs.wso2.org/wiki/display/ESB460/VFS+Transport
http://docs.wso2.org/wiki/pages/viewpage.action?pageId=16846489
what is the error you got?
Anyway the endpoint you gave looks wrong. It should start with the transport protocol as vfs://
eg: vfs:file:///home/desktop/append.txt