I have REST API URL defined as
https://hostname/[version]/[service]/[operation]
and I would like to transform JSON payload in WSO2 API Manager in order to put version and operation inside.
So the WSO2 will send it to backend in the following form:
POST https://backend/[service]
{
“version”: [version],
“operation”: [operation]
}
So I need to put version and operation from URL to JSON payload. How to do it?
As I understand you want to create a new payload and POST with values from the resource URI definition (not API context).
Lets assume:
you don't want to change the method (the resource is having POST method).
the original request posted is JSON as well
You may want to create a (synapse) mediation flow as for ESB building such a message.
Plugging in the mediation is described in the documentation Adding Mediation Extensions. This mediation will be executed for every request of the API
Having resource https://hostname/[version]/[service]/[operation]
In the mediation, you may build the JSON payload
<payloadFactory media-type="json">
<format>{
"version":"$1",
"service": "$2",
"some_original_data": "$3"
}</format>
<args>
<arg expression="get-property('uri.var.version')"/>
<arg expression="get-property('uri.var.service')"/>
<arg expression="$.person.name" evaluator="json" />
</args>
</payloadFactory>
Related
My API is receiving a XML with a field that repeats itself a couple of times, looking like this:
<Root>
<Element>
<Request>username=username&password=password&field3=field3&field4=field4</Request>
<Request>username=username&password=password&field3=field3&field4=field4</Request>
</Element>
</Root>
Using an IterateMediator I am extracting those Request fields and make separate calls with each of them to an URL, with the help of an AggregatorMediator and aSendMediator.
The API I'm calling will only accept only one line at a time, along with the following metadata:
HTTP method: POST
Content-Type: application/x-www-form-urlencoded
Payload: username=username&password=password&field3=field3&field4=field4
The problem is that, although I extract the payload properly from the IterateMediator, the value of the $body before reaching the SendMediator is surrounded with Soap Envelope tags. I've tried using a PayloadFactoryMediator to change the value of the $body, but I can only make it xml or json, and not text/plain.
Do you have any idea how can I make the body something else than xml or json?
Could you show your sequence?
"..but I can only make it xml or json, and not text/plain.."
Did you try media-type="text" ?
<payloadFactory media-type="text" xmlns="http://ws.apache.org/ns/synapse">
<format>$1</format>
</payloadFactory>
I receive from an rabbitmq inbound an XML message and I have to send it to mongodb with restheart.
I must modify the content of the Json generate by the formatter before sending it to the mongo rest api, because I must modify a datetime format send in string to add "ISOdate ()" for mongodb.
My incomming message can be different from other messages.
For exemple, I have this from inbound :
<CONTENT><CODE_USER>100</CODE_USER><DATE>2017-12-12</DATE></CONTENT>
and I get this for the endpoint,
{"CONTENT" : {"CODE_USER":100","DATE":"2017-12-12"}}
So I want to modify the Json format before sending to the endpoint to modify the date string.
Is there a way to trigger the esb Json formatter and get the result before send it to the endpoint ?
Thanks,
Nicolas
I find an solution,
I use json-eval($.*.) function and an script mediator to modify the date format before sending to the endpoint, like this :
<property expression="json-eval($.*.)" name="location" scope="default" type="STRING"/>
<script language="js"><![CDATA[var message = new String(mc.getProperty('location'));
var reg = /\"((\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)\.(\d{1,3})Z)\"/g;
var mess=message.replace (reg,'{\"$date\" : \"\$1\"}');
mess=mess.substring (1,mess.length-1);
mc.setPayloadJSON(mess);]]></script>
<property name="ContentType" scope="axis2" type="STRING" value="application/json"/>
Yes that is possible. You can add a mediator to the in-sequence such that the transformation happens before releasing the response to mongo-db.
Please check the available mediators and if there is no any suitable one for your requirement, you can simply write your own mediator by extending org.apache.synapse.mediators.AbstractMediator
For the details on how to write a custom mediator, you can refer the blog: https://medium.com/#nirothipanram/esb-few-tips-in-writing-a-custom-class-mediator-b9a322f4eaa8
I am relative new in WSO2 to create services, and i have to do a project in the enterprise where i work. First i had to take a message in, for example, json and send xml. I was able to do so, with this code:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/Test" name="JsonToXmlApi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST" uri-template="/xml/">
<inSequence>
<property name="messageType" scope="axis2" type="STRING" value="text/xml"/>
<header action="remove" name="To" scope="default"/>
<property name="RESPONSE" scope="default" type="STRING" value="true"/>
<send/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Now i need to do the following, i have to send a message in SOAP, XML format, consume it in SOAP and receive back with JSON or XML format.
Someone could help me? I am asking because i am searching for tutorials, for beginners, about it for more than a week, and wasn't able to solve my problem.
Another question, would be possible to do this in REST API project?
If someone could help me, with a detailed tutorial or good video teaching that, i would be grateful.
Thank you.
Your post is a bit unclear but I will try to summarise the typical common combinations of input interface, wso2 component and backend services when you are developing web services.
Web services are services you can invoke over http or https - you might have three types of interfaces for these and you might use two types of WSO2 ESB components - proxy and API - to develop these.
1) Input/Output: JSON
WSO2 component: API
Backend: Any (JSon/XML/SOAP)
2) Input/Output: Plain XML
WSO2 component: API
Backend: Any (JSon/XML/SOAP)
3) Input/Output: SOAP
WSO2 component: Proxy
Backend: Any (JSon/XML/SOAP)
One of the key components of any SOAP service is the WSDL - the WSO2 proxy allows you to publish a WSDL so that your caller knows what sort of data and operations your service provides.
See more here: https://docs.wso2.com/display/EI611/Working+with+Proxy+Services
Adding this note only because you asked:
Technically, you might be able to receive a SOAP message in an API component because SOAP is just an XML document - but it would be really pointless. The purpose of the API component is to expose resources and allow you to perform HTTP operations (GET, POST etc) on those resources (this model can be called REST).
The purpose of a SOAP based web service is to expose data and operations as defined in a WSDL and the WSO2 proxy component is what allows you to do that.
I would to publish a SOAP service as REST(json) API . The service operation I want to expose has a xml sequence element in the WSDL/XSD definition:
<xs:complexType name="hellolist">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="hellolistResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
I don't know how to map a JSON array to a xml sequence using PayloadFactory. All the samples I found deal only with simple json and SOAP messages like this sample WSO2 transformation.
I would like to transform this json message:
{"hellolist":{"name":["Peter","Mary","Ann","James"]}}
To this soap message:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:dum="http://dummyservice2.com/">
<soapenv:Header/>
<soapenv:Body>
<dum:hellolist>
<name>Peter</name>
<name>Mary</name>
<name>Ann</name>
<name>James</name>
</dum:hellolist>
</soapenv:Body>
</soapenv:Envelope>
You can use payload factory mediator to do this. It can be used to transform or replace message content in between the client and the back-end server. In the case of your scenario you can configure a proxy service as below.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="json_to_xml_factory"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
<target>
<inSequence>
<payloadFactory media-type="xml">
<format>
<dum:hellolist xmlns:dum="http://dummyservice2.com/">
<name xmlns="">$1</name>
<name xmlns="">$2</name>
<name xmlns="">$3</name>
<name xmlns="">$4</name>
</dum:hellolist>
</format>
<args>
<arg evaluator="json" expression="$.hellolist.name[0]"/>
<arg evaluator="json" expression="$.hellolist.name[1]"/>
<arg evaluator="json" expression="$.hellolist.name[2]"/>
<arg evaluator="json" expression="$.hellolist.name[3]"/>
</args>
</payloadFactory>
<log level="full"/>
</inSequence>
<outSequence/>
</target>
<description/>
</proxy>
Please refer below links for more information on this.
https://docs.wso2.com/display/ESB490/PayloadFactory+Mediator
http://christinetechtips.blogspot.com/2014/02/payload-factory-mediators-to-work-with.html
http://madhukaudantha.blogspot.com/2013/05/wso2-esb-payload-mediator-tutorial.html
If you have a non-static payload (your case), payload-factory-mediator will not be the solution (only for static payload). The best way for your kind of a problem is using xslt mediator + enrich mediator in WSO2 ESB. You can try out this example, https://docs.wso2.com/display/ESB481/Sample+440%3A+Converting+JSON+to+XML+Using+XSLT
Or you can use script mediator in case, as explained in this example.https://docs.wso2.com/display/ESB481/Sample+350%3A+Introduction+to+the+Script+Mediator+Using+JavaScript . Yet not the most preffered way.
I have done your kind of conversions using WSO2 ESB as I described above. But I don't know whether those mediator tools are also available with WSO2 APIM (I couldn't find any example when I googled for your problem).
Try the steps mention below
Steps to Convert a SOAP Test service in SoapUI with assertions into JSON service with assertions
Copy the project xml file of SOAP UI project
Open the copied xml file
Find the format for all assertion types in JSON that is equal in SOAP call assertion
Now convert all the soap assertions in the config node of the test step node to JSON assertion config node format as found in the previous step
Convert the SOAP call request into JSON call by changing all the fields as below
• Add the output_format node
• Add the Rest call – service name
• Remove the nodes without value and convert the others into JSON format like {“node name” : “value”} etc.,
In the test step node type attribute – it will be “request” change it to “httprequest”
Save the file and open it in SOAP UI
Now you can see all the services in the newly opened project will be in JSON format and all assertions will be available that were in SOAP call previously.
also refer the link which helps to convert the request completely including the assertions
please reply if it is not helpful
From WSO2 ESB 4.8 - json is supported natively, information can be found in WSO2 websit. Referring below blog w.r.t.json formatter & builder
http://charithaka.blogspot.co.uk/2013/10/the-difference-between-json-streaming.html
My use-case is to use JSON format over HTTP/1.1 REST, main point to note here is:
not to tranform the json data within ESB (keep the json format natively within ESB)
manipulate the json using mediator in json format (mediator which support json natively - script/custom mediators)
trying to use :
org.apache.axis2.json.JSONStreamBuilder
org.apache.axis2.json.JSONStreamFormatter
as mentioned in Charitha blog. Now, I am not seeing json being converted to soap in the soap message (using Log Mediator).
does that mean, WSO2 ESB 4.8 does not convert a json request to soap under the hood avoiding data loss for some data format (e.g., Array, etc., as mentioned in https://github.com/erny/jsonbuilderformatter)
Can anyone share me an example of logging / manipulating json message using org.apache.axis2.json.JSONStreamBuilder & org.apache.axis2.json.JSONStreamFormatter (or) any other builder and formatter for json without converting into soap message in the message context.
As mentioned in http://docs.wso2.org/display/ESB480/JSON+Support doc, from ESB 4.8 onwards you have to use following builder and formatter to keep the JSON representation intact without converting to XML. These two are the default option in ESB 4.8
org.apache.synapse.commons.json.JsonStreamBuilder
org.apache.synapse.commons.json.JsonStreamFormatter
To log as JSON use,
<log>
<property name="JSON-Payload" expression="json-eval($.)"/>
</log>
You can manipulate the JSON body using Payload Factory or Script mediators. For e.g.-
<payloadFactory media-type="json">
<format>
{
"location_response" : {
"name" : "$1",
"tags" : $2
}
}
</format>
<args>
<arg evaluator="json" expression="$.name"/>
<arg evaluator="json" expression="$.types"/>
</args>
</payloadFactory>
Refer the documentation for more details.