How to pass through ServiceCallout response content in FaultRule?

I have a ServiceCallout that calls a RESTful service to authenticate a user and the service it calls returns a 401 response code if the request is "bad", which are treated as errors since I didn't override the success.codes property on the HTTPTargetConnection element.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="authenticate-user" continueOnError="false" enabled="true">
    <DisplayName>AuthenticateUser</DisplayName>
    <Request clearPayload="true">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <Set>
            <FormParams>
                <FormParam name="grant_type">password</FormParam>
                <FormParam name="username">{request.formparam.username}</FormParam>
                <FormParam name="password">{request.formparam.password}</FormParam>
            </FormParams>
            <Verb>POST</Verb>
        </Set>
    </Request>
    <Response>AuthenticateUserResponse</Response>
    <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="api"/>
        </LoadBalancer>
        <Path>/authenticate</Path>
    </HTTPTargetConnection>
</ServiceCallout>

What I am trying to do is pass-through the ServiceCallouts response (populated in the AuthenticateUserResponse variable) to the following FaultRule, which invokes an AssignMessage policy.

<FaultRule name="AuthenticateUserFailedFaultRule">
            <Step>
                <Name>create-fault-response</Name>
            </Step>
            <Condition>(fault.name = "ErrorResponseCode") or (fault.name = "ExecutionFailed")</Condition>
        </FaultRule>

In the AssignMessage policy, I have tried using {AuthenticateUserResponse}, {AuthenticateUserResponse.Content}, {AuthenticateUserResponse.Body} and those values are always null...

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="create-fault-response" continueOnError="false" enabled="true">
    <DisplayName>CreateFaultResponse</DisplayName>
    <Set>
        <StatusCode>401</StatusCode>
        <ReasonPhrase>Unauthorized</ReasonPhrase>
        <Payload contentType="application/json">{AuthenticateUserResponse}</Payload>
    </Set>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>

Is there a way to get the ServiceCallout's response content and use it in an AssignMessage policy that is invoked from a FaultRule?

Solved Solved
0 3 1,556
1 ACCEPTED SOLUTION

@Anthony Coelho

Hi Anthony, I understood your problem.

Can you just check what is the response from the service call out when you fire request with incorrect data.

And then in the Assign message header, use the proper variable name like

AuthenticateUserResponse.content

View solution in original post

3 REPLIES 3

adas
Participant V

@Anthony Coelho I am not sure in which order you are calling these policies but ideally it should be Service Callout>Assign Message>Fault Rule. Typically, if a policy encounters an error it would skip the flow and directly go to the fault rule depending on how you have defined your fault rules. I think in this case the Assign Message is not even getting executed. So please set the continueOnError="true" for the service callout policy so that it goes into the AssignMessage policy. Then you would be able to perform the required checks. In the fault rule you can check the status code or some other condition, throw an error and abort the flow.

So basically, setting the continueOnError flag to true in the service callout policy should help you. Here's the code snippet from one of my own proxies:

    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>QuotaPerMinute</Name>
            </Step>
            <!--Section added for AuthAPI Service Callout -->
            <Step>
                <Name>PrepareAuthCalloutRequest</Name>
            </Step>
            <Step>
                <Name>AuthAPIServiceCallout</Name>
            </Step>
            <Step>
                <Name>PopulateAuthCalloutResponse</Name>
            </Step>
            <Step>
                <Condition>(AuthAPIResponse.status.code!=200)</Condition>
                <Name>RaiseFaultAuthAPI</Name>
            </Step>
            <!--Section ends -->
        </Request>
        <Response/>
    </PreFlow>

@Anthony Coelho

Hi Anthony, I understood your problem.

Can you just check what is the response from the service call out when you fire request with incorrect data.

And then in the Assign message header, use the proper variable name like

AuthenticateUserResponse.content

Hey @gbhandari, that was it. "Content" is case-sensitive...