routing to target points

Not applicable

Hello,

I am trying to route to a target endpoint using route rules, but unable to do so.

I am putting the condition: <RouteRule name="MyRoute"> <Condition>request.header.routeTo = "TargetEndpoint-1"</Condition> <TargetEndpoint>TargetEndpoint-1</TargetEndpoint> </RouteRule>

in pre flow and naming the target end point as "TargetEndpoint-1".

The error which I see on web page is: Cannot GET /TargetEndpoint-1.

Please help

0 14 2,545
14 REPLIES 14

Not applicable

Hi @Prateek Gautam,

The route rule condition should not go in the pre-flow.

To route to different target your proxy XML should be sth like :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <Description/>
    <FaultRules/>
    <PreFlow name="PreFlow">
        <Request/>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
    <HTTPProxyConnection>
        <BasePath>/routerule-36920</BasePath>
        <Properties/>
        <VirtualHost>secure</VirtualHost>
        <VirtualHost>default</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="Target-2">
        <Condition>request.queryparam.targetTo= "Target2"</Condition>
        <TargetEndpoint>TargetEndpoint-2</TargetEndpoint>
    </RouteRule>
    <RouteRule name="my backend">
        <TargetEndpoint>TargetEndpoint-1</TargetEndpoint>
    </RouteRule>
</ProxyEndpoint>

To Read more about rout rules Click Here.

Hope this helps!!

Hello Vipul thanks for the help. I tried to apply your code but still got some error. Would you please take a look at my code and check what issues would be there?

Thanks

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<Description/>
<FaultRules/>
<PreFlow name="PreFlow">
<Request>
<Step>
<Name>Spike-Arrest-1</Name>
</Step>
</Request>
<Response/>
</PreFlow>
<PostFlow name="PostFlow">
<Request/>
<Response>
<Step>
<Name>XML-to-JSON-1</Name>
</Step>
</Response>
</PostFlow>
<Flows/>
<HTTPProxyConnection>
<BasePath>/v1/hello</BasePath>
<Properties/>
<VirtualHost>secure</VirtualHost>
<VirtualHost>default</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="MyRoute">
<Condition>request.queryparam.targetTo= "TargetEndpoint-1"</Condition>
<TargetEndpoint>TargetEndpoint-1</TargetEndpoint>
</RouteRule>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
</ProxyEndpoint>

Hi @Prateek Gautam ,

From your post, it sounds like you are putting the RouteRule inside the preflow? The RouteRule should be at the top level inside ProxyEndpoint. Otherwise, the RouteRule looks fine if you have a target named TargetEndpoint-1 and you supply the correct routeTo header in the request.

Also, "Cannot GET /TargetEndpoint-1" sounds like /TargetEndpoint-1 might be your basepath?

If you are still having issues, posting more of your proxy code here might help in debugging your issue. Good luck!

Hello, thanks for your help. I think the problem is in my code. Would you please have a look at it?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<Description/>
<FaultRules/>
<PreFlow name="PreFlow">
<Request>
<Step>
<Name>Spike-Arrest-1</Name>
</Step>
</Request>
<Response/>
</PreFlow>
<PostFlow name="PostFlow">
<Request/>
<Response>
<Step>
<Name>XML-to-JSON-1</Name>
</Step>
</Response>
</PostFlow>
<Flows/>
<HTTPProxyConnection>
<BasePath>/v1/hello</BasePath>
<Properties/>
<VirtualHost>secure</VirtualHost>
<VirtualHost>default</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="MyRoute">
<Condition>request.queryparam.targetTo= "TargetEndpoint-1"</Condition>
<TargetEndpoint>TargetEndpoint-1</TargetEndpoint>
</RouteRule>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
</ProxyEndpoint>

Not applicable

Hi Prateek,

1. in pre flow and naming the target end point

the RouteRule should in the next level of <ProxyEndpoint>.

<HTTPProxyConnection> <BasePath>/mock-target-api</BasePath> <Properties/> <VirtualHost>default</VirtualHost> <VirtualHost>secure</VirtualHost> </HTTPProxyConnection> <RouteRule name="search"> <TargetEndpoint>default</TargetEndpoint> <Condition>(proxy.pathsuffix MatchesPath "/products") and (request.verb = "GET")</Condition> </RouteRule> <TargetEndpoint>default</TargetEndpoint> </RouteRule> </ProxyEndpoint>

2.The error which I see on web page is: Cannot GET /TargetEndpoint-1.

You should be using a incorrect request url.

To find the correct request url of your api. you can go to overtime tab of your api.

Hello Seam,

Would you please look at my code and try to figure out what the problem might be?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
<Description/>
<FaultRules/>
<PreFlow name="PreFlow">
<Request>
<Step>
<Name>Spike-Arrest-1</Name>
</Step>
</Request>
<Response/>
</PreFlow>
<PostFlow name="PostFlow">
<Request/>
<Response>
<Step>
<Name>XML-to-JSON-1</Name>
</Step>
</Response>
</PostFlow>
<Flows/>
<HTTPProxyConnection>
<BasePath>/v1/hello</BasePath>
<Properties/>
<VirtualHost>secure</VirtualHost>
<VirtualHost>default</VirtualHost>
</HTTPProxyConnection>
<RouteRule name="MyRoute">
<Condition>request.queryparam.targetTo= "TargetEndpoint-1"</Condition>
<TargetEndpoint>TargetEndpoint-1</TargetEndpoint>
</RouteRule>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>
</ProxyEndpoint>

And please tell me to run the route rule and see different target end point what should we type in the url in web page?

How did you passed the route rule paramter, are you using postman or curl?

it is curl

Not applicable

Could you sent out your curl request url ?

would you tell me how to put url on webpage. like if I want to go to other target point then how will I do it or what url should I do to?

Not applicable
@Prateek Gautam

Could you check what are you passing in your queryparam?

As you mention the condition.

<RouteRule name="MyRoute">
<Condition>request.queryparam.targetTo= "TargetEndpoint-1"</Condition>
<TargetEndpoint>TargetEndpoint-1</TargetEndpoint>
</RouteRule>
<RouteRule name="default">
<TargetEndpoint>default</TargetEndpoint>
</RouteRule>

Please pass the request as

 http://prateekgautam-test.apigee.net/v1/hello?targetTo=TargetEndpoint-1 

Yeah it worked. Thanks a lot Ummang!!!

Also if you don't pass any queryparam by default as per your Route rule condition request will reach to default targetendpoint.

PS: Please accept the answer if its resolved your query.