Handle if-else condition in a proxy

Not applicable

I want to perform a if-else operation in api proxy.

Meaning, I would want the first policy to execute on the main url that has been configured while proxy creation.

If the return from that call is an error or a status code other than 200, then I would want a series of another policies to be executed and at the end of this chain, execute the main url again.

How can I achieve such if-else condition

0 7 2,213
7 REPLIES 7

Looking at your requirements, the thing that came to mind is that you could push all this in one API proxy. However by design it might be good to look at all of your API's and then see if maybe some functions can be combined in order to reduce the amount of API calls.

But anyway, when you need more than one service/API call, you can either try it with conditions on your API policies. OR use the javascript policy to execute multiple API calls.

For instance, checking the outcome of a ServiceCallout Policy like so:

(servicecallout.serviceCalloutQueryParser.failed = "true")

See: http://apigee.com/docs/api-services/reference/conditions-reference

Or using Javascript:

See: http://apigee.com/docs/api-services/content/httpclient-javascript-object

Hope this helps,

Not applicable

Hi @apigee1234567 ,

One way is to invoke the target endpoint again via a service callout policy at the target response. Also, add a condition to the policy to enforce only on non-2xx.

Note: You might also need to configure the property "success.codes" in the target endpoint. Please refer to http://apigee.com/docs/api-services/reference/endpoint-properties-reference for details on "success.codes"

Regards,

Rampradeep.

Hello @Rampradeep Krishnamurthy

I did follow the same approach as suggested by you

Here is the code

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="default">
    <Description/>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>GlobalSessionToken</Name>
            </Step>
            <Step>
                <Name>Assign-Message-2</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <Flows>
        <Flow name="Flow-1">
            <Description/>
            <Request/>
            <Response>
                <Step>
                    <Name>GetOTP</Name>
                </Step>
                <Step>
                    <Name>FetchOTP</Name>
                </Step>
                <Step>
                    <Name>Service-Callout-1</Name>
                </Step>
                <Step>
                    <Name>Extract-Variables-1</Name>
                </Step>
                <Step>
                    <Name>KVM-Put-2</Name>
                </Step>
                <Step>
                    <Name>Assign-Message-1</Name>
                </Step>
                <Step>
                    <Name>Service-Callout-2</Name>
                </Step>
            </Response>
            <Condition>response.status.code != 200</Condition>
        </Flow>
    </Flows>
    <PostFlow name="PostFlow"/>
    <HTTPTargetConnection>
        <URL>https://datadirect.factset.com/services/NewsFetch</URL>
    </HTTPTargetConnection>
</TargetEndpoint>

However, when I execute the proxy, when the response is not 200, it is not going into the conditional flow to move things forward.

It just stops at the request.

Am I doing anything wrong here?

What is the response code in this case? If the response code is > 399, you would need to set the success.codes in the target endpoint. The community post https://community.apigee.com/questions/5200/does-setting-successcodes-on-targetendpoint-overwr.html talks about the success.codes property.

Example:

HTTPTargetConnection>

<Properties>

<Property name="success.codes">1xx,2xx,3xx,400,500</Property>

</Properties>

<URL>http://weather.yahooapis.com</URL>

</HTTPTargetConnection>

@Rampradeep Krishnamurthy

Yes you were right. I had to add 401 as the success code for the flow to move forward.

The flow did go into the return policies. However, I ran into 1 more problem.

The last Service Callout policy in the response did not execute because the server says

"Reason: ResponseCode 401 is treated as error"

How can I make the Service Callout policy execute despite receiving 401? or any other response code?

Hi @apigee1234567,

To ignore the servicecallout error, you would need to set the flag continueOnError to true. By default, continueonError is false.

<ServiceCallout async="false" continueOnError="true" enabled="true" name="Service-Callout-1">

Regards,

Rampradeep.

You can add conditional flows in your proxy. Main call will go to one flow where desired policy will execute and then hit to taget. If it returns other than 200, you can make service callout to same proxy but another flow where other policies will execute and then hit to target.