I have created an API in the WSO2 ESB (4.8.1) and I wanted to send a PUT request to that API with a request body. I have tried with the sample
and I wanted to log a property values in the insequence of the defined API.
This is my request body:
This is the way how I tried to log the location name:
But I’m getting an error like this:
(ERROR - SynapseJsonPath #stringValueOf. Error evaluating JSON Path . Returning empty result. Error>>> invalid path)
So how can I read these values?
To achieve your requirement, you should send the "Content-Type" HTTP header with the request like below,
"Content-Type : application/json"
enter image description here
Then you can log the specific JSON element like below.
<log>
<property name="location" expression="json-eval($.coordinates.location[0].name)"></property>
</log>
Then you can see following log,
enter image description here
Thanks.
If you want to get single variable from user in json request you can use this code
Use This json:
{
"namee":"UsmanYaqooooooooooob"
}
Api Code:
<api xmlns="http://ws.apache.org/ns/synapse" name="Hello" context="/hello">
<resource methods="POST" uri-template="/post">
<inSequence>
<log level="custom">
<property name="===========inSequence" value="****"></property>
<property name="locationsssssssss" expression="json-eval(.namee)"></property>
</log>
<payloadFactory media-type="json">
<format>{"hello":"world"}</format>
<args></args>
</payloadFactory>
<property name="messageType" value="text/xml"></property>
<respond></respond>
</inSequence>
</resource>
</api>
Related
I'building a proxy-service using wso2 esb. It sends a REST request to Google Books API and receives json. In this Json, there's a dynamic array which I have to parse to XML. I can't seem to figure out how can I do that.
received json payload
"items": [
{
"volumeInfo": {
"title": "Web Services",
"authors": [
"Gustavo Alonso",
"Fabio Casati",
"Harumi Kuno",
"Vijay Machiraju"
],
"publisher": "Springer Science & Business Media",
"publishedDate": "2003-09-04"
]
}
If you glance over the above received Json, it's something like this items[0].authors[i]
here authors[i] is very dynamic, as different books have different number of authors.
How can I convert this payload to XML and then send as an XML to client
<items>
<titie></title>
<authors>
<author></author>
<author></author>
<author></author>
.
.
.
</authors>
</items>
When converting from JSON to XML I almost always use this way.
First set messageType to xml, you may also set ContentType but I am not 100% sure if it is required
<property name="messageType" scope="axis2" value="application/xml"/>
<property name="ContentType" scope="axis2" value="application/xml"/>
Next use the payload factory with an enclosing root element, set the media type to XML
<payloadFactory media-type="xml">
<format>
<SomeRequest xmlns="yourXMLNamespace">
$1
</SomeRequest>
</format>
<args>
<arg evaluator="json" expression="."/>
</args>
</payloadFactory>
Now you will have something that looks like this.
<SomeRequest>
<items>
<volumeInfo></volumeInfo>
<title>Web Services</title>
<authors>Gustavo Alonso</authors>
<authors>Fabio Casati</authors>
<authors>Harumi Kuno</authors>
<authors>Vijay Machiraju</authors>
<publisher>Springer Science & Business Media</publisher>
<publishedDate>2003-09-04</publishedDate>
</items>
<items>
...
</items>
</SomeRequest>
See how it unwrapped the JSON array, it created multiple elements all using the array name. To get from here to the response format you want the easiest way to do it is using an xslt transform.
<xslt key="{name of your xslt transform file}"/>
Then you can respond to back to the client.
It may be worthwile checking out the JSON Support page in the wso2 docs. It covers how JSON is converted to and from XML
https://docs.wso2.com/display/EI620/JSON+Support
In the outsequence you can use a payload mediator to construct the xml from JSON.
https://docs.wso2.com/display/ESB500/PayloadFactory+Mediator
I have a below mentioned like JSONPayload and I want to select the JSON object having hotelcode "ALE1_LON" from the "content" array.
For this I have used "$.content[?(#.hotelcode='ALE1_LON')]" JSONPath expression as mentioned below in the property mediator.
<property expression="json-eval($.content[?(#.hotelcode='ALE1_LON')])" name="hotelContet" scope="default" type="STRING"/>
<payloadFactory media-type="json">
<format>$1</format>
<args>
<arg evaluator="xml" expression="get-property('hotelContet')"/>
</args>
</payloadFactory>
<respond/>
Problem is, this returns an empty array.
But when I try same payload and same JSONPath with an online JSONPath Tester it says my JSONPath is correct (sending below like JSON Object).
[
{
"hotelcode":"ALE1_LON",
"hotelname":"Alexandra"
}
]
Why this is not working in WSO2 ESB Propery Mediator?
Is this because of the JSONPath version?
WSO2 is not supported this way?
Do I need to iterate each element and filter?
JSON Payload :
{
"_id":"INV27_1112",
"_rev":"5-876038bf65752ce4505e50baea6d5581",
"content":[
{
"hotelcode":"AMB3_LON",
"hotelname":"Ambassadors Bloomsbury"
},
{
"hotelcode":"ALE1_LON",
"hotelname":"Alexandra"
},
{
"hotelcode":"ALO_LON",
"hotelname":"Aloft London Excel"
}
]
}
WSO2 ESB version : 5.0.0
Note : "json-eval($.content[0])" like exprssions return results correctly
References :
WSO2 ESB refferred JSONPath article
WSO2 ESB Doc on JSON Support
Error is with the json-eval expression. == must be used for equality not =. eg : json-eval($.content[?(#.hotelcode=='ALE1_LON')]) works fine. Thank you for the support.
Reference : https://odieweblog.wordpress.com/2015/09/20/new-json-path-expression-predicates/, http://madhukaudantha.blogspot.com/2017/02/wso2-esb-with-javascript-object-notation.html
Following is the JSON trying add in payload factory mediator(wso2 esb 4.8.1):
{
"root":"<abc>
<ab>
<id>361</id>
<name>What’s your number</name>
<age>number</age>
</ab>
</abc>"
}
When i add this in payload factory mediator succesfully added but when re-open again xml tags are not there only JSON payload is there like below:
{"root":""}
I need to know how to add these kind of payloads in WSO2 ESB as a payload/request for any one of the servcie.
The problem is that you have xml structure inside json. Try this payloadFactory:
<payloadFactory media-type="json">
<format>
{"root":
{"abc":
{"ab":
{"id":"362","name":"What’s your number","age":"number"}
}
}
}
</format>
<args/>
</payloadFactory>
You can find more information here: https://docs.wso2.com/display/ESB481/JSON+Support
I am using wso2 ESB for creating a rest API in json format. I have a restful json webservice which gives the response. I need to use one of the parameter of this response in another service call.
How can i do this kind of service chaining in wso2 ESB.
for ex:-
i have a restful url as abc.com/cusotmer. I got the response back. Suppose "id" is one of the parameter of response.
I want to use this parameter in another service call (say, xyz.com/sheet) which internally calls the first service (abc.com/cusotmer).
Could any of you please help me in this regard ?
This is service chaining. You can refer to WSO2 library article which explains this clearly trough a example
http://wso2.com/library/articles/2011/01/wso2-esb-by-example-service-chaining/
Service chaining is an important feature in any of the ESBs avaiable.
You can use Call Mediator which keeps the control in the sequence (say your insequence)
You can use call for first endpoint , for second endpoint i am assuming the id is available in response body, you can either use json-eval or xpath to get this value, depending on type of data second service uses you can use PayloadFactoryMediator and set id in desired body part
And in next line you can call again using Call Mediator or Send
A rough code will be like.
<payloadFactory media-type="json">
<format>
{ "A": "6", "tests": [{ "id": "xyz", "status": "new", "emp": [{ "Id": "12345" }] }], "student": [{ "Id": "65", "Name": "Ram" }] }
</format>
</payloadFactory>
<call>
<endpoint>
<http method="get" uri-template="http://192.168.1.10:8088/mockaxis2service"/>
</endpoint>
</call>
<!-- suppose id field comes as response in field name id2 -->
<payloadFactory media-type="json">
<format>
{
"inp2second":"$1"
}
</format>
<args>
<arg expression="$.emp.id2"/>
</args>
</payloadFactory>
<call> or <send>
You can use either call or send mediator now ,send mediator to move the control to outsequence.
for json expression used above as $.emp.id2 kindly refer json support page for example
An example of service chaining is also available here
I have the following camel route :
<camelContext trace="false" id="blueprintContext" xmlns="http://camel.apache.org/schema/blueprint">
<route id="addressRoute">
<from uri="cxfrs:bean:addressSearchEndpoint"/>
<removeHeaders pattern="CamelHttp*"/>
<recipientList>
<simple>http4://<someURL>/PostcodeAnywhere/Interactive/RetrieveByAddress/v1.20/json.ws?Key=<someKey>&Address=WR2%206NJ&Company=&UserName=PROVI11136</simple>
</recipientList>
<log message="${body}"/>
</route>
which uses a recipient list to invoke an external REST service.
The issue I am having is that although I see the response in the log:
[ qtp269562434-42] addressRoute INFO
[{"Udprn":"52269655","Company":"","Department":"","Line1":"2 Wylcotts","Line2":"Moseley Road","Line3":"Hallow","Line4":"","Line5":"","PostTown":"Worcester","County":"Worcestershire","Postcode":"WR2 6NJ","Mailsort":"94141","Barcode":"(WR26NJ4D7)","Type":"Residential"}]
it does not appear in the browser when I invoke
http://localhost:9191/cxf/addresssearch/
Anything I am missing?
Thanks
I solved by removing the log tag at the end.