JavaRegex for serviceCallout.Response

Not applicable

Hi, I want to do a condition match in my conditional flow to not execute a step /policy if the serviceCallout.response contains (anything preceding)403(anything following) in the response from backend server. Since payload from server is JSON based it can contain characters like "{, comma, : " example -> {"error" : 403, "key": value, "Key2" :value2 etc. }

so something like below would be incorrect but I need correct expression.

(serviceCallout.response JavaRegex "*403 *" )

0 8 642
8 REPLIES 8

you could use the following regexp

serviceCalloutResponse.content JavaRegex '^\{.*"error": (\d+).*$'

Hi Miren,

That gives error while deploying, I tried following as I need a match on {"error":403, anything......}

(serviceCallout.response JavaRegex '^\{.*"error":403(\d+).*$')

Do I need to escape quotation " ?

error -> The revision is deployed, but traffic cannot flow. Expected a pattern as the RHS of a pattern match expression

Thanks,

Mayank

I have tried out the expression I wrote before and it workd for me

serviceCalloutResponse.content JavaRegex '^\{.*"error": (\d+).*$'

The expression is in between single quotes so the double quotes do not need to be escaped

So this is full expresison in the flow <Step> <Condition> (response.status.code != "403") or (serviceCallout.response JavaRegex '^\{.*"error":403.*$ ) </Condition><Name>AssignMessage.CreateResponse</Name> </Step> . I am still getting error not sure what is causing this.

@Mayank

Since the check is on the response body the flow variable to use is serviceCalloutResponse.content and not serviceCalloutResponse.response

Please have a look here at the flow variables of the ServiceCallout policy

http://docs.apigee.com/api-services/reference/service-callout-policy#flowvariables

Hi @Mayank

Service Callout policy throws an error if it receives from the target service HTTP codes 4XX or 5XX. You might want to set the success codes to HTTPTargetConnection as mentioned here. . Once you have set the success.codes, your condition will be executed. Please note that service callout uses its own response object, so your condition of response.status.code != "403" might not work. You can use servicecallout.{policy-name}.failed or {policy_response_name}.status.code and the expression as mentioned by Miren.

<Step>
   <Condition> (serviceCallout.status.code != "403")</Condition>
   <Name>AssignMessage.CreateResponse</Name>
</Step>

Service callout policy sample code below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="service-callout">
    <DisplayName>service-callout</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="request">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>serviceCalloutResponse</Response>
    <HTTPTargetConnection>
        <Properties>
            <Property name="success.codes">1xx,2xx,400,403,404,500</Property>
        </Properties>
        <URL>{URL}</URL> 
    </HTTPTargetConnection>
</ServiceCallout>

Thanks that's good to know but it seems currently backend service is sending http 200 for error 403. I just placed that condition as a safeguard. Anyway the problem here is error during deployment, way before user sends request. It seems the javaregex expression is n't recognized by Apigee during deployment .

I tried adding the condition as Miren mentioned. I am able to save and deploy the proxy

<Step>
	<Name>set-target-server-path</Name>
	<Condition>(serviceCallout.response JavaRegex '^\{.*"error":403(\d+).*$')</Condition>
</Step>

How ever I am not sure what error you are getting. The other option I can think is to get the response, extract it using the Extract Variable policy and then raise a Fault if the extracted variable has an error.

If you can paste the exact error response payload, I might be able to help you with the policies.