Is it possible to check the size of an array as part of a flow condition?

We have a proxy and would like to have the flow either fire:

1. A raisefault policy in the case that an array (of errors obtained from the target response) is not empty

2. The standard assign message response policy if that error array is empty.

It should be noted that for most (400/404-type) errors, the http response code sent back by the target is actually a 200.

Thanks.

1 4 2,368
4 REPLIES 4

@Sean Case , Yes, It's possible. You can do same using JavaScript policy. Set a flow variable using context.setVariable("arrayEmpty", true/false) in JS policy after verifying array length & then use same in policy conditions.

@Anil Sagar, thanks! I was wondering if there was a way to do it without any js. The suggestion you provided would be fairly simple to implement, though.

@Sean Case , AFAIK, Computation logic can be done only using Apigee Extension policies like python / JS / Java OR If out of the box any policy supports same like ExtractVariable policy to extract part of response / request. You cannot do computation inside XML elements of a policy. That means the result should be readily available part of response / request to directly use same in conditions via flow variables. You can do this in target server itself & send the boolean result back either part of response headers / payload to avoid using JS policy.

Right after I posted this question, I thought of one possible solution.

I added an extract variables policy which checked for the first element in that error array:

<Variable name="ErrorsIndex">
            <JSONPath>$.Errors[0]</JSONPath>
        </Variable>

I then did a conditional in the flow to check if that (at least one) element was null:

<Step>
                    <Name>AssignMessages-FeedbackResponse</Name>
                    <Condition>ErrorsIndex = null</Condition>
                </Step>
                <Step>
                    <Name>RaiseFault-FeedbackErrorResponse</Name>
                    <Condition>ErrorsIndex != null</Condition>
                </Step>

Which avoided having to add any Javascript. It may not be and ideal solution, though, depending on situation. Thanks for your assistance!