Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Combining JS policy+ ExtractVariables policy + ServiceCallout policy

Not applicable

Hi everyone,

I'm introducing myself to apigee, and I'm trying to make some chaining with different policies.

I've added to my Proxy Endpoint postFlow, callout to an external service, extraction of some values obtained in the previous step and JS policies in order to combine the response of the callout and the main response.

I'm not getting the chaining of these policy but I'm not sure where is the problem

If I trace my api proxy I can see the body of the service callout response with the expected response, which is like that:

<?xml version="1.0"?>
<CUSTOMER xmlns:xlink="http://www.w3.org/1999/xlink">
    <ID>1</ID>
    <FIRSTNAME>Susanne</FIRSTNAME>
    <LASTNAME>King</LASTNAME>
    <STREET>366 - 20th Ave.</STREET>
    <CITY>Olten</CITY> 
</CUSTOMER>

My Configuration is like that:

PostFlow:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <Description/>
    <FaultRules/>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>Spike-Arrest-1</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response>
            <Step>
                <Name>XML-to-JSON-1</Name>
            </Step>
            <Step>
                <Name>Service-Callout-1</Name>
            </Step>
            <Step>
                <Name>Extract-Variables-1</Name>
            </Step>
            <Step>
                <Name>JS-cambiaSaludo</Name>
            </Step>
        </Response>
    </PostFlow>
    <Flows/>
    <HTTPProxyConnection>
        <BasePath>/v1/hellopolicies</BasePath>
        <Properties/>
        <VirtualHost>default</VirtualHost>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>
</ProxyEndpoint>

XML-to-JSON:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-1">
    <DisplayName>XML to JSON-1</DisplayName>
    <Properties/>
    <Format>yahoo</Format>
    <OutputVariable>response</OutputVariable>
    <Source>response</Source>
</XMLToJSON>

Callout:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>customerCallout</DisplayName>
    <Request variable="myRequest"/>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <URL>http://www.thomas-bayer.com/sqlrest/CUSTOMER/1/</URL>
    </HTTPTargetConnection>
</ServiceCallout>

Extract Variable:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
    <DisplayName>Extract Variables-1</DisplayName>
    <Source>calloutResponse</Source>
    <VariablePrefix>customerResponse</VariablePrefix>
    <XMLPayload>
        <Variable name="id" type="integer">
            <XPath>/CUSTOMER/ID</XPath>
        </Variable>
        <Variable name="nombre" type="string">
            <XPath>/CUSTOMER/FIRSTNAME</XPath>
        </Variable>
        <Variable name="apellido" type="string">
            <XPath>/CUSTOMER/LASTNAME</XPath>
        </Variable>
        <Variable name="city" type="string">
            <XPath>/CUSTOMER/CITY</XPath>
        </Variable>
        <Variable name="city" type="string">
            <XPath>/CUSTOMER/STREET</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

JS policy config

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="JS-cambiaSaludo">
    <DisplayName>JS-cambiaSaludo</DisplayName>
    <Properties/>
    <ResourceURL>jsc://JS-cambiaSaludo.js</ResourceURL>
    <IncludeURL>jsc://pretty.js</IncludeURL>
</Javascript>

JS(Source code)

//Parse the respose from the target.
var res = JSON.parse(context.proxyResponse.content);
//Pull out only the information we want to see in the response.
var estado = res.root.state;
if (estado == "CA"){
    estado = "California";
}
var formatedResponse =  "\n" + 
                        "\n" + 
                        "\n" + 
                        "El resultado de la consulta es el siguiente:" + "\n" + 
                        "\n" + 
                        "Ciudad: " + res.root.city + "\n" + 
                        "Estado: " + estado + "\n" + 
                        "Nombre: " + res.root.firstName + "\n" + 
                        "Apellido: " + res.root.lastName + "\n" + 
                        "PrettyDate: " + prettyDate("2016-12-18T22:24:17Z") + 
                        "\n" + 
                        "\n" + 
                        "\n" + customerResponse;
//Set the response variable. 
context.proxyResponse.content = formatedResponse;

so,

What am I doing wrong? I think there is something I have not understood...

How can I extract the values from the callout result, for using it in my JS policy?.

Thanks in advance

Best regards.

Solved Solved
0 5 870
1 ACCEPTED SOLUTION

Hi @J M,

Kindly go through this post - Fetching Servicecallout response

Have used assign message policy instead of JS policy.

Let me know if you need any help.

Keep us posted, thank you

View solution in original post

5 REPLIES 5