How to apply conditional or branching logic in an API backend call

cboyd
Participant II

Hi All,

I want to apply some conditional logic to the result of a backend call.

If the backend call (#1) returns 200 then I want it to call another API (#2).

If the backend call (#1) returns 404 or 500 then I want that error to be passed to the caller and the stream terminates.

Any idea what the best way to do this is?

Thanks.

Solved Solved
0 7 255
1 ACCEPTED SOLUTION

sidd-harth
Participant V

Well on the response side you could use a Service callout(SC) policy with a Condition which checks the status code if it is 200 it is going to call another API. If it is anything other than 200 the SC will be skipped an error message will be sent to the client.

<Condition>response.status.code = 200</Condition>

View solution in original post

7 REPLIES 7

sidd-harth
Participant V

Well on the response side you could use a Service callout(SC) policy with a Condition which checks the status code if it is 200 it is going to call another API. If it is anything other than 200 the SC will be skipped an error message will be sent to the client.

<Condition>response.status.code = 200</Condition>

Hi @Siddharth Barahalikar, thanks for the reply. Sorry I wasn't able to respond yesterday other priorities took up the time.

So I have played with this today and I haven't been able to get this to work. Here is my Service call out:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>Service Callout-1</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties/>
        <URL>http://dummy.restapiexample.com/api/v1/employee/73744</URL>
        <Condition>response.status.code equal 200</Condition>
        <Name>Raise-Fault-1</Name>
    </HTTPTargetConnection>
</ServiceCallout>

Regardless of what I change the either the condition isn't getting triggered or the "Raise-Fault-1" isn't getting invoked.

Any thoughts about where I may be going awry?

Thanks!

You need to use the condition outside the policy.

<Response>           
 <Step>
   <Name>Service-Callout-1</Name>
   <Condition>response.status.code = 200</Condition>
 </Step>
</Response>

Hi @Siddharth Barahalikar,

I put the condition outside the policy and it still seems to be ignoring the condition:

    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>verify-api-key</Name>
            </Step>
            <Step>
                <Name>am-apikey-remove</Name>
            </Step>
            <Step>
                <Name>Extract-Variables-1</Name>
            </Step>
            <Step>
                <Name>Service-Callout-1</Name>
                <Condition>response.status.code = 200</Condition>
                <Name>Raise-Fault-1</Name>
            </Step>
            <Step>
                <Name>JavaScript-1</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>

It should be added on Response Side as menttioned in my previous comment.

<Response>
<Step>
                <Name>Service-Callout-1</Name>
                <Condition>response.status.code = 200</Condition>

            </Step>
</Response>

use raise fault if you want to sent custom error message.

Hi @Siddharth Barahalikar Thanks! That did it.

Sorry I was being thick about the <Response></Response> location.

Again, thanks for your time and patience!

@cboyd

The reason it didn't work is the place where you included the condition is incorrect. You should include the condition in the place where you have the service callout included, like the Response. I also notice there are two <Name> tags in the same Step.

<Response><br>  <Step><br>  <Name>Service-Callout-1</Name>
<Condition>response.status.code equal 200</Condition>
</Step>
</Response>