I am not able to use extracted variable in Apigee Flow.

Not applicable

I am trying to extract variable from the request and assign the extracted variable to the response. But in assign message the variable extracted is not accessible. Please help. Here's the relevant code.

The ExtractMessage policy:

<ExtractVariables name="Extract-Variables-1"> 
  <Source>request</Source> 
  <XMLPayload> 
    <Namespaces> 
      <Namespace prefix="dir">urn:43BFF88D-D204-4427-B6BA-140AF393142F</Namespace> 
    </Namespaces> 
    <Variable name="travelmode" type="string"> 
      <XPath>/dir:Directions/dir:route/dir:leg/dir:step/@mode</XPath> 
    </Variable> 
    <Variable name="duration" type="string"> 
      <XPath>dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:value</XPath> 
    </Variable> 
    <Variable name="timeunit" type="string"> 
      <XPath>/dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:text</XPath> 
    </Variable> 
  </XMLPayload> 
</ExtractVariables>

AssignMessage policy:

<AssignMessage name="Assign-Message-1"> 
  <AssignVariable> 
    <Name>appSecret</Name>
    <Value>Data2</Value> 
  </AssignVariable> 
  <AssignVariable>
    <Name>AssignVar</Name> 
    <Value>Data1</Value> 
  </AssignVariable> 
  <AssignTo
      createNew="false" type="response"/> 
  <Set> 
    <Payload>
      <Tamatar>{appSecret}</Tamatar> 
      <Aloo>{AssignVar}</Aloo> 
    </Payload>
  </Set> 
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage>


I am trying to extract data from the request by using Extract message policy and then want to return one of the extracted variables into the response. Hence extract is in the PREFLOW and Assign Policy is in the Post Flow.

The Request XML is :

<Directions xmlns="urn:43BFF88D-D204-4427-B6BA-140AF393142F"> 
  <status>OK</status> 
  <route> 
    <summary>I-40 W</summary> 
    <leg> 
      <step mode="DRIVING"> 
        <start_location> 
          <lat>41.8507300</lat> 
          <lng>-87.6512600</lng> 
        </start_location> 
        <end_location> 
          <lat>41.8525800</lat> 
          <lng>-87.6514100</lng> 
        </end_location> 
        <duration> 
          <value>19</value> 
          <text>minutes</text> 
        </duration> 
      </step> 
    </leg> 
  </route> 
</Directions>


Thanks,

Nishith

http://nishithc-test.apigee.net/sudo-backend

1 8 1,633
8 REPLIES 8

@Nishith Coomar , Welcome to Apigee Community.

What do you mean extracted variable is not accessible? Can you post your Assign message policy? Ideally, you need to use Extract variable policy to extract variable & use assign message policy to add same to response.

Not applicable

Thanks a lot for replying to this. The extrack message policy is :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1"> <Source>request</Source> <XMLPayload> <Namespaces> <Namespace prefix="dir">urn:43BFF88D-D204-4427-B6BA-140AF393142F</Namespace> </Namespaces> <Variable name="travelmode" type="string"> <XPath>/dir:Directions/dir:route/dir:leg/dir:step/@mode</XPath> </Variable> <Variable name="duration" type="string"> <XPath>dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:value</XPath> </Variable> <Variable name="timeunit" type="string"> <XPath>/dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:text</XPath> </Variable> </XMLPayload> </ExtractVariables>

Assign message policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1"> <AssignVariable> <Name>appSecret</Name> <Value>Data2</Value> </AssignVariable> <AssignVariable> <Name>AssignVar</Name> <Value>Data1</Value> </AssignVariable> <AssignTo createNew="false" type="response"/> <Set> <Payload> <Tamatar>{appSecret}</Tamatar> <Aloo>{AssignVar}</Aloo> </Payload> </Set> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> </AssignMessage>

I am trying to extract data from the request by using Extract message policy and then want to return one of the extracted variables into the response. Hence extract is in the PREFLOW and Assign Policy is in the Post Flow.

The Request XML is :

<Directions xmlns="urn:43BFF88D-D204-4427-B6BA-140AF393142F"> <status>OK</status> <route> <summary>I-40 W</summary> <leg> <step mode="DRIVING"> <start_location> <lat>41.8507300</lat> <lng>-87.6512600</lng> </start_location> <end_location> <lat>41.8525800</lat> <lng>-87.6514100</lng> </end_location> <duration> <value>19</value> <text>minutes</text> </duration> </step> </leg> </route> </Directions>

Thanks,

Nishith

Not applicable

Thanks a lot for replying to this @Anil Sagar.

I've updated the question above to include code.

Thanks,

Nishith

Hi @Nishith Coomar

there are a couple potential problems with your code

  1. there is a leading slash missing in one of the xpath expressions
  2. maybe you are anchoring your ExtractVariables policy in the wrong place.

Attached please find a working example.

I used this kind of flow in the proxy:

    <Flow name='test extract variables'>
      <Request>
        <Step><Name>EX-1</Name></Step>
      </Request>
      <Response>
        <Step><Name>AssignMessage-1</Name></Step>
      </Response>
      <Condition>(proxy.pathsuffix MatchesPath "/t1") and (request.verb = "POST")</Condition>
    </Flow>

As you can see, I did not use PreFlow or PostFlow. I used a conditional flow. Be aware that Preflow does not refer only to the Request, and Postflow does not apply only to the response. Pre and Post indicate that the policies in those sections run before or after any conditional flow. In other words, it is possible to have a policy configured on the postflow for the request, or as a preflow on the response. If this is not clear to you, see the documentation.

I hope this helps.

nishith-extract.zip

@Dino

Thanks for you response, it provided me with a great insight in understanding of the Pre, Post and conditional flow.

I did try the solution you have provided. While trying with the Request XML I still can not see the extraction happening rather the below response is returned:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <status />
  <mode />
  <duration />
  <timeunit />
</root>
which suggest that either the variables were not extracted or they were not referred in the assign message policy. I need your help in resolving this issue. 

@Dino: Thanks for your help. very nice insight provided regarding the pre, post and conditional flows. Thanks again 🙂

Glad to help!

Not applicable

@Raghavendra:

I have created the same example ,but getting empty response.

Here are the details:

Proxy.xml:ExtractVar-ServiceCallTesting

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <APIProxy revision="1" name="ExtractVar-ServiceCallTesting"> <Basepaths>/extractvar-servicecalltesting</Basepaths> <ConfigurationVersion majorVersion="4" minorVersion="0"/> <CreatedAt>1477398859216</CreatedAt> <CreatedBy>nvraghavendra254@gmail.com</CreatedBy> <Description/> <DisplayName>ExtractVar-ServiceCallTesting</DisplayName> <LastModifiedAt>1477402295052</LastModifiedAt> <LastModifiedBy>nvraghavendra254@gmail.com</LastModifiedBy> <Policies> <Policy>Assign-Message-1</Policy> <Policy>Extract-Variables-1</Policy> </Policies> <ProxyEndpoints> <ProxyEndpoint>default</ProxyEndpoint> </ProxyEndpoints> <Resources/> <Spec/> <TargetServers/> <TargetEndpoints/> <validate>false</validate> </APIProxy>

Extract variables:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" name="Extract-Variables-1"> <!--<Source>request</Source>--> <VariablePrefix>extracted1</VariablePrefix> <!--<XMLPayload> <Namespaces> <Namespace prefix="dir">urn:43BFF88D-D204-4427-B6BA-140AF393142F</Namespace> </Namespaces> <Variable name="travelmode" type="string"> <XPath>/dir:Directions/dir:route/dir:leg/dir:step/@mode</XPath> </Variable> <Variable name="duration" type="string"> <XPath>dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:value</XPath> </Variable> <Variable name="timeunit" type="string"> <XPath>dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:text</XPath> </Variable> </XMLPayload>--> <XMLPayload> <Namespaces> <Namespace prefix="dir">urn:43BFF88D-D204-4427-B6BA-140AF393142F</Namespace> </Namespaces> <Variable name="status" type="string"> <XPath>/dir:Directions/dir:status/text()</XPath> </Variable> <Variable name="travelmode" type="string"> <XPath>/dir:Directions/dir:route/dir:leg/dir:step/@mode</XPath> </Variable> <Variable name="duration" type="string"> <XPath>/dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:value/text()</XPath> </Variable> <Variable name="timeunit" type="string"> <XPath>/dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:text/text()</XPath> </Variable> </XMLPayload> </ExtractVariables>

Assign message :Assign-Message-1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1"> <!-- <AssignVariable> <Name>appSecret</Name> <Value>{timeunit}</Value> </AssignVariable> <AssignVariable> <Name>AssignVar</Name> <Value>{duration}</Value> </AssignVariable>--> <AssignTo createNew="false" type="response"/> <!-- <Set> <Payload> <Tamatar>appSecret</Tamatar> <Aloo>AssignVar</Aloo> </Payload> </Set>--> <Set> <Payload contentType="application/xml" variablePrefix="{" variableSuffix="}"> <root> <status>{extracted1.status}</status> <mode>{extracted1.travelmode}</mode> <duration>{extracted1.duration}</duration> <timeunit>{extracted1.timeunit}</timeunit> </root> </Payload> </Set> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </AssignMessage>

Note : I have added above to policies under Proxy endpoint ,post flow.

Request :

<Directions xmlns="urn:43BFF88D-D204-4427-B6BA-140AF393142F"> <status>OK</status> <route> <summary>I-40 W</summary> <leg> <step mode="DRIVING"> <start_location> <lat>41.8507300</lat> <lng>-87.6512600</lng> </start_location> <end_location> <lat>41.8525800</lat> <lng>-87.6514100</lng> </end_location> <duration> <value>19</value> <text>minutes</text> </duration> </step> </leg> </route> </Directions>

please look into this and correct me ,if it required any changes