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

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 Like

@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.

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

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> request urn:43BFF88D-D204-4427-B6BA-140AF393142F /dir:Directions/dir:route/dir:leg/dir:step/@mode dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:value /dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:text

Assign message policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> appSecret Data2 AssignVar Data1 {appSecret} {AssignVar} false

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 :

OK

I-40 W <start_location> 41.8507300 -87.6512600 </start_location> <end_location> 41.8525800 -87.6514100 </end_location> 19 minutes

Thanks,

Nishith

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

1 Like

@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 :slight_smile:

Glad to help!

@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"?> /extractvar-servicecalltesting 1477398859216 nvraghavendra254@gmail.com ExtractVar-ServiceCallTesting 1477402295052 nvraghavendra254@gmail.com Assign-Message-1 Extract-Variables-1 default false

Extract variables:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> extracted1 urn:43BFF88D-D204-4427-B6BA-140AF393142F /dir:Directions/dir:status/text() /dir:Directions/dir:route/dir:leg/dir:step/@mode /dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:value/text() /dir:Directions/dir:route/dir:leg/dir:step/dir:duration/dir:text/text()

Assign message :Assign-Message-1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> {extracted1.status} {extracted1.travelmode} {extracted1.duration} {extracted1.timeunit} true

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

Request :

OK

I-40 W <start_location> 41.8507300 -87.6512600 </start_location> <end_location> 41.8525800 -87.6514100 </end_location> 19 minutes

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