How to routes 2 or more TargetEndPoints based on request body using Postman in apigee?

Hi Team,

I have a usecase where I have 2 TargetEndPoints.

My first TargetEndPoint is :
http://www.mocky.io/v2/5cb569053300002e165d7a96

Response for this Target is : application/json

{ "Details" : { name: "Rishu", age: 22, city: "India" }}

My second TargetEndPoint is :
http://www.mocky.io/v2/5cb5697833000020185d7a99

Response for this Target is : application/xml

Rishu Singh

Now when I’m sending 1st Post Request from Postman :

{
“target” : “json”,
“response” : “xml”
}

My Expected Response is : (It Must hit the TargetEndpoint 1 and convert it as Xml )

22 India Rishu

Now when I’m sending 2nd Request :

{
“target” : “json”,
“response” : “json”
}

My Expected Response is : (It Must hit the TargetEndpoint 1 and return as same as json response )

{ “Details” : { name: “Rishu”, age: 22, city: “India” }}

Now when I’m sending 3nd Request :

{
“target” : “xml”,
“response” : “json”
}

My Expected Response is : (It Must hit the TargetEndpoint 2 and convert it as Json )

{ “Books”:
{
“JavaScript”: “Rishu”,
“Paython”: “Singh”
}}

Now when I’m sending Post 4nd Request from Postman :

{
“target” : “xml”,
“response” : “xml”
}

My Expected Response is : (It Must hit the TargetEndpoint 2 and return as same as xml )\

Rishu Singh

In my ProxyEndpoint to route request I used :

request.content = "application/json" ApigeeeMockyIo request.content = "application/json" ApigeeNew default

And I used JsonToXml Policy on response side of TargetEndpoint1(ApigeeeMockyIo) ,Similarly I used XmlToJson Policy on response side of TargetEndpoint2(ApigeeNew).
But On every hit I’m getting FaultError .

Any Advice what’s wrong here?
Is I need to add any other policy for this work?

Thanks

Rishika

Could someone advise me?

@Anil Sagar @ Google,@Dino-at-Google,@Brendan,@deboraelkin,@Siddharth Barahalikar,@Priyadarshi Ajitav Jena,@Robert Johnson,@AnilSagar,@sudheendras,@Mukundha Madhavan,@Jeremy Whitlock,@Nagashree B

Hi @Rishika Kumari,

Regarding using multiple Route, please read this -
https://docs.apigee.com/api-platform/fundamentals/understanding-routes; I see in your example, you have used same condition <Condition>request.content = "application/json"</Condition> for both the route rule, so you know it wont work, right? Also value of request.content would be the request payload, I am not sure if that’s what you want to check for this condition?

I would suggest, If it is just a plain transformation, technically you do not have to use multiple routes; you can get the JSON and XML response and vice versa using JSONtoXML and XMLtoJSON policies, that’s what they meant for, rather building/using a different back-end APIs for it.

Looking at the response above, I would recommend using a single flow with this backend URL, i.e.

http://www.mocky.io/v2/5cb569053300002e165d7a96 and put a single JSONToXML policy (in proxy Response PreFlow) to convert the response to XML (of course with condition, you’re free to choose one, I am suggesting to have condition let say ;

<Condition>request.header.Accept = "application/xml"</Condition>

Further, I see your backend API anyway responds with JSON default, even if you skip passing request header, so just having a single JSONToXML should be fine.

Attached is the sample proxy - 67933-rev2-2019-04-16.zip, Import this bundle in your apigee edge org, deploy to an environment; enable tracing and and use below curl command to see this in action;

curl -X GET \
  [https://{your-apigee-proxy-endpoint}/67933/v2/5cb569053300002e165d7a96](https://{your-apigee-proxy-endpoint}/67933/v2/5cb569053300002e165d7a96) \
  -H 'Accept: application/xml'
curl -X GET \
  [https://{your-apigee-proxy-endpoint}/67933/v2/5cb569053300002e165d7a96](https://{your-apigee-proxy-endpoint}/67933/v2/5cb569053300002e165d7a96) \
  -H 'Accept: application/json'
2 Likes

I don’t know fully what is wrong, but this;

<RouteRule name="MyRoute">
  <Condition>request.content = "application/json"</Condition>
  <TargetEndpoint>ApigeeeMockyIo</TargetEndpoint>
</RouteRule>

…looks wrong. The request.content is probably not going to be “application/json”.

I think you want

<Condition>request.header.content-type = "application/json"</Condition>
1 Like

Thanks @Kuldeep Bhati,@Dino-at-Google,@Siddharth Barahalikar

But when I tried to add more than 2 target and route the request between them,

To Route request i used :

  <RouteRule name="MyRoute">
  <Condition>request.header.content-type = "application/json"</Condition>
  <TargetEndpoint>ApigeeeMockyIo</TargetEndpoint>
  </RouteRule>
  <RouteRule name="MyRouteNew">
  <Condition>request.header.content-type = "application/xml"</Condition>
  <TargetEndpoint>ApigeeNew</TargetEndpoint>

I’m getting Error message :

{
  "fault": {
  "faultstring": "Unable to route the message to a Target Endpoint",
  "detail": {
  "errorcode": "messaging.runtime.RouteFailed"
  }
  }
}

Hi

@Kuldeep Bhati,@Anil Sagar @ Google,@Sudhee Sreedhara,@Priyadarshi Ajitav Jena,

@Mukundha Madhavan

And I’m sending “post” request using body which i mentioned in my question.

1st Post Request from Postman :

{
“target” : “json”,
“response” : “xml”
}

My Expected Response is : (It Must hit the TargetEndpoint1(target) and convert it as Xml response

22 India Rishu

Now when I’m sending 2nd Request :

{
“target” : “json”,
“response” : “json”
}

My Expected Response is : (It Must hit the TargetEndpoint1(target) and return as same as json i.e orignal response.

{ “Details” : { name: “Rishu”, age: 22, city: “India” }}

And vice-versa, when i use target as xml.

Thanks for your support.

Hi Community,

I need some guidance on how to achieve the requirement.

Thanks in advance

Rishika

@Dino-at-Google,@Kuldeep Bhati,@Anil Sagar @ Google,@Sudhee Sreedhara,@Priyadarshi Ajitav Jena,@Mukundha Madhavan,@Siddharth Barahalikar