Transaction policy condition check

My Question is i have two conditions to check in the response of an transaction to be considered successful and those are 

statuscode that comes in header and and status message that come as Success in the JSON response body. I could not find a way to check both these conditions  before marking the transaction a success.

Could any one help to solve this?

 

@anilsagar 

0 2 160
2 REPLIES 2

I could not find a way to check both these conditions before marking the transaction a success.

I am not sure what "marking the transaction a success" entails. I guess you want to use a Condition?

If you want to conditionally execute a policy in your flow, then you can use a Condition with 2 tests, AND'ed together. Something of this form:

 

  <Step>
    <Condition>TEST1 AND TEST2</Condition>
    <Name>NAME-OF-POLICY-HERE</Name>
  </Step>

 

...where each TEST1 and TEST2 follow the form _variablename_ operator _value_.

Now you just have to figure out how to write the tests. The first one, testing the status code, is easy:

 

response.status.code = 200 

 

You can read about the response.status.code variable in the variables reference.

The second test, in which you want to examine the JSON payload, is a little trickier. There is no "operator" in the list of operators supported by the Condition that can peek into the JSON payload and check a field. So you need to do it in two steps.

  1. Extract the specific field of interest into a variable
  2. test THAT variable.

Suppose the JSON payload in the success case is like this:

 

{
  "result" : "ok"
}

 

And maybe in the failure case it's like this:

 

{
  "error" : "some specific error message or code here"
}

 

Then you can use an ExtractVariables policy to get the "result" field into a context variable. Like this:

 

<ExtractVariables name='EV-Result'>
  <Source>response</Source>
  <VariablePrefix>extracted</VariablePrefix>
  <JSONPayload>
    <Variable name='result'>
       <JSONPath>$.result</JSONPath>
    </Variable>
  </JSONPayload>
</ExtractVariables>

 

After executing this policy, the context variable extracted.result will hold "ok" in the first case, and will be null in the second (error) case. OK now that we have a context variable, we can add it into the condition. The result is like this:

 


  <Step>
    <Name>EV-Result</Name>
  </Step>
  <Step>
    <Condition>(response.status.code = 200) AND (extracted.result = "OK")</Condition>
    <Name>NAME-OF-OTHER-POLICY-HERE</Name>
  </Step>

 

 

@dchiesa1  Apologies as I was not clear in my question. My question was regarding transaction recording policy which we add for a product. It is not at a proxy level when i can use the AND condition . 

Configure a transaction recording policy  |  Apigee Edge  |  Apigee Docs

In the Transaction Success Criteria field, specify the expression based on the value of the Status attribute (described next) for determining when the transaction is successful (for charging purposes)

So we have this success criteria to determine if the transaction is success or not that is where i want the AND condition to work.

transaction policy.png