Hi Team,
I have a usecase where I have to convert my request data JsonToXml and XmlToJson.
My First TargetEndPoint is : (JsonEndPoint)
[http://www.mocky.io/v2/5cb592b9330000f5175d7bfe](http://www.mocky.io/v2/5cb592b9330000f5175d7bfe)
Response for this First Target is : application/json
{ "Details" : { name: "Rishu", age: 22, city: "India" }}
My Second TargetEndPoint is : (XmlEndPoint)
[http://www.mocky.io/v2/5cbe9fa7300000f2069ce1b7](http://www.mocky.io/v2/5cbe9fa7300000f2069ce1b7)
Response for this First Target is : application/xml
<Books>
<C>Er. Smitha Swetha</C>
<Java>Dr. Ranjan Singh</Java>
<Paython>Mr. Abdul Kaadir</Paython>
</Books>
Now I’m sending 1st Post Request from Postman :
{
"target" : "json",
"response" : "Json"
}
// Getting Correct Response which is My TargetEndPoint-1(JsonEndPoint) data.
Now I’m sending 2nd Post Request from Postman :
{
"target" : "Xml",
"response" : "Xml"
}
// Getting Correct Response which is My TargetEndPoint-2(XmlEndPoint)data.
Now when I’m sending 3rd Post Request from Postman :
{
"target" : "json",
"response" : "Xml"
}
Getting an Error:
{
"fault": {
"faultstring": "Execution of JsonToXmlJs failed with error: Javascript runtime error: "TypeError: Cannot read property "response" from undefined. (JsonToXmlJs.js:5)"",
"detail": {
"errorcode": "steps.javascript.ScriptExecutionFailed"
}
}
}
My Expected Output is below : (It Must hit the TargetEndpoint-1(JsonEndPoint) and convert it as Xml and give response back )
<Details>
<age>22</age>
<city>India</city>
<name>Rishu</name>
</Details>
Now when I’m sending 4th Post Request from Postman :
{
"target" : "Xml",
"response" : "Json"
}
Getting Error
{
"fault": {
"faultstring": "Execution of JsonToXmlJs failed with error: Javascript runtime error: "TypeError: Cannot read property "response" from undefined. (JsonToXmlJs.js:5)"",
"detail": {
"errorcode": "steps.javascript.ScriptExecutionFailed"
}
}
}
My Expected Output is below : (It Must hit the TargetEndpoint-2(XmlEndPoint) and convert it as Json and give response back )
{
"Books": {
"C": "Er. Smitha Swetha",
"Java": "Dr. Ranjan Singh",
"Paython": "Mr. Abdul Kaadir"
}
}
First JavaScript code is : In ProxyEndPoint Preflow Request :
var body = request.content.asJSON;
context.setVariable("target", body.target);
var v = context.getVariable('target');
print(v);
Second JavaScript code is : In ProxyEndPoint Preflow Request :
var body = request.content.asXML;
context.setVariable("target", body.target);
var v = context.getVariable('target');
print(v);
Third JavaScript code is : In ProxyEndPoint Preflow Response :
var body = response.content.asJSON;
context.setVariable("response", body.response);
var v1 = context.getVariable('response');
print(v1);
Fourth JavaScript code is : In ProxyEndPoint Preflow Response :
var body = response.content.asXML;
context.setVariable("response", body.response);
var v1 = context.getVariable('response');
print(v1);
RouteRule Condition Is :
<RouteRule name="MyRoute">
<Condition>target = "json"</Condition>
<TargetEndpoint>JsonEndPoint</TargetEndpoint>
</RouteRule>
<RouteRule name="MyRouteNew">
<Condition>target = "xml"</Condition>
<TargetEndpoint>XmlEndPoint</TargetEndpoint>
</RouteRule>
I Used JsonToXml Policy In TargetEndPoint-1(JsonEndPoint) Preflow Response with Condition :
<Response>
<Step>
<Name>JsonToXml</Name>
<Condition>(response.header.Content-type = "application/json") and (response = "json")</Condition>
</Step>
</Response>
And XmlToJson Policy In TargetEndPoint-2(XmlEndPoint) Preflow Response with Condition :
<Response>
<Step>
<Name>JsonToXml</Name>
<Condition>(response.header.Content-type = "application/xml") and (response = "xml")</Condition>
</Step>
</Response>
Condition for all my javascript in ProxyEndPoint :
<PreFlow name="PreFlow">
<Request>
<Step>
<Name>JsonJs</Name>
<Condition>(request.header.Content-type = "application/json")</Condition>
</Step>
<Step>
<Name>XmlJs</Name>
<Condition>(request.header.Content-type = "application/xml")</Condition>
</Step>
</Request>
<Response>
<Step>
<Name>JsonToXmlJs</Name>
<Condition>(response.header.Content-type = "application/json")</Condition>
</Step>
<Step>
<Name>XmlToJsonJs</Name>
<Condition>(response.header.Content-type = "application/xml")</Condition>
</Step>
</Response>
</PreFlow
Any Advice what’s wrong here?
Could Someone please help me out?
Is I need to add any other policy for this work?
@Anil Sagar @ Google,@Dino-at-Google,@Brendan,@deboraelkin,@Siddharth Barahalikar,@Priyadarshi Ajitav Jena,@Robert Johnson,@AnilSagar,@sudheendras,@Mukundha Madhavan,@Jeremy Whitlock,@Nagashree B
Please let me know how to resolve this issue.
Thanks in Advance.
Rishika
