extracts variable from URI and assignmessage

Not applicable

Requirement:I want to fetch data from URI using Extract variables and display this as response using assign message.

I have created a proxy with no endpoint.Used 'Extract Variables-1' to extract from request url 'http://apitechbasics-prod.apigee.net/extractvarsample-uripath/vehicles/1234' and use assign message to display it as final response .I tried different ways ,but none of them worked .

Proxy:

<ProxyEndpoint name="default"> 
  <Description/> 
  <FaultRules/> 
  <PreFlow name="PreFlow"> 
    <Request> 
      <Step> 
        <Name>Extract-Variables-1</Name> 
      </Step> 
      <Step> 
        <Name>Assign-Message-1</Name> 
      </Step> 
    </Request> 
    <Response/> 
  </PreFlow> 
  <PostFlow name="PostFlow"> 
    <Request/> 
    <Response/> 
  </PostFlow> 
  <Flows/> 
  <HTTPProxyConnection> 
    <BasePath>/extractvarsample-uripath</BasePath> 
    <Properties/> 
    <VirtualHost>default</VirtualHost> 
    <VirtualHost>secure</VirtualHost> 
  </HTTPProxyConnection> 
  <RouteRule name="noroute"/> 
</ProxyEndpoint>

Extract Variables-1:

<ExtractVariables name="Extract-Variables-1"> 
  <DisplayName>EV-Extract Variables-URIPath</DisplayName> 
  <Properties/> 
  <Source>request</Source> 
  <URIPath> 
    <Pattern ignoreCase="true">/vehicles/{VIN}</Pattern> 
  </URIPath> 
  <VariablePrefix>pathurireq</VariablePrefix>
</ExtractVariables>

I am able to extract and put into 'pathurireq' but to display in response where I am facing issues.

Assign-Message-1:

<AssignMessage name="Assign-Message-1"> 
  <DisplayName>AM-Assign Message-URIPath</DisplayName> 
  <AssignVariable> 
    <Name>name</Name> 
    <Value/> 
    <Ref>pathurireq.id</Ref> 
  </AssignVariable> 
  <Set> 
    <Payload> 
      <Tamatar>{name}</Tamatar> 
      <Aloo>{name}</Aloo> 
    </Payload> 
  </Set> 
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> 
  <AssignTo createNew="false" transport="http" type="response"/> 
</AssignMessage>
Solved Solved
1 6 2,470
1 ACCEPTED SOLUTION

Two issues here.

1. you are assigning to the response message in the Request preflow.

The response message gets reset at inception of the Response flow, which happens AFTER the Request flow ends.

So you need to move your assignmessage to the response flow.

like this:

<ProxyEndpoint name="default"> 
  <Description/> 
  <FaultRules/> 
  <PreFlow name="PreFlow"> 
    <Request> 
      <Step> 
        <Name>Extract-Variables-1</Name> 
      </Step> 
    </Request> 
    <Response> 
      <Step> 
        <Name>Assign-Message-1</Name> 
      </Step> 
    </Response>
  </PreFlow> 
  <PostFlow name="PostFlow"> 
    <Request/> 
    <Response/> 
  </PostFlow> 
  <Flows/> 
  <HTTPProxyConnection> 
    <BasePath>/extractvarsample-uripath</BasePath> 
    <Properties/> 
    <VirtualHost>default</VirtualHost> 
    <VirtualHost>secure</VirtualHost> 
  </HTTPProxyConnection> 
  <RouteRule name="noroute"/> 
</ProxyEndpoint>

2. Your AssignMessage isn't quite right Modify it like so:

<AssignMessage name="Assign-Message-1"> 
  <Set> 
    <Payload contentType='text/xml'> 
      <Tamatar>{pathurireq.VIN}</Tamatar> 
      <Aloo>{pathurireq.VIN}</Aloo> 
    </Payload> 
  </Set> 
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> 
  <AssignTo createNew="false" transport="http" type="response"/> 
</AssignMessage>

The pathurireq is the prefix you have used in the ExtractVariables. And the VIN is the name for the field you are extracting. Hence the name of the variable you want to reference here is pathurireq.VIN . You should be able to use the Trace UI to verify that this variable has been set and holds the value you think it should hold.

Can you try that?

View solution in original post

6 REPLIES 6

Just one comment - you can format the code you paste in by using the code button.

4351-code-button.png

Click THAT, and THEN paste in the XML.

If you do that, then instead of this:

4352-screenshot-20170215-094956.png

...we will all see something more readable, like this:

<ProxyEndpoint name="default"> 
  <Description/> 
  <FaultRules/> 
  <PreFlow name="PreFlow"> 
    <Request> 
      <Step> 
        <Name>Extract-Variables-1</Name> 
      </Step> 
      <Step> 
        <Name>Assign-Message-1</Name> 
      </Step> 
    </Request> 
    <Response/> 
  </PreFlow> 
  <PostFlow name="PostFlow"> 
    <Request/> 
    <Response/> 
  </PostFlow> 
  <Flows/> 
  <HTTPProxyConnection> 
    <BasePath>/extractvarsample-uripath</BasePath> 
    <Properties/> 
    <VirtualHost>default</VirtualHost> 
    <VirtualHost>secure</VirtualHost> 
  </HTTPProxyConnection> 
  <RouteRule name="noroute"/> 
</ProxyEndpoint>

I've modified your post to reformat the XML. But in the future, it would help if posters could do that on their own.

Two issues here.

1. you are assigning to the response message in the Request preflow.

The response message gets reset at inception of the Response flow, which happens AFTER the Request flow ends.

So you need to move your assignmessage to the response flow.

like this:

<ProxyEndpoint name="default"> 
  <Description/> 
  <FaultRules/> 
  <PreFlow name="PreFlow"> 
    <Request> 
      <Step> 
        <Name>Extract-Variables-1</Name> 
      </Step> 
    </Request> 
    <Response> 
      <Step> 
        <Name>Assign-Message-1</Name> 
      </Step> 
    </Response>
  </PreFlow> 
  <PostFlow name="PostFlow"> 
    <Request/> 
    <Response/> 
  </PostFlow> 
  <Flows/> 
  <HTTPProxyConnection> 
    <BasePath>/extractvarsample-uripath</BasePath> 
    <Properties/> 
    <VirtualHost>default</VirtualHost> 
    <VirtualHost>secure</VirtualHost> 
  </HTTPProxyConnection> 
  <RouteRule name="noroute"/> 
</ProxyEndpoint>

2. Your AssignMessage isn't quite right Modify it like so:

<AssignMessage name="Assign-Message-1"> 
  <Set> 
    <Payload contentType='text/xml'> 
      <Tamatar>{pathurireq.VIN}</Tamatar> 
      <Aloo>{pathurireq.VIN}</Aloo> 
    </Payload> 
  </Set> 
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> 
  <AssignTo createNew="false" transport="http" type="response"/> 
</AssignMessage>

The pathurireq is the prefix you have used in the ExtractVariables. And the VIN is the name for the field you are extracting. Hence the name of the variable you want to reference here is pathurireq.VIN . You should be able to use the Trace UI to verify that this variable has been set and holds the value you think it should hold.

Can you try that?

@Dino :Thanks for your response.

I am not able to extract variable from URI path 'http://apitechbasics-prod.apigee.net/extractvarsample-uripath/vehicles/1222'.Here is the policy that I have used .

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-Extract-Variables-URIPath">

<DisplayName>Extract Variables-1</DisplayName>

<Properties/>

<Source>request</Source>

<URIPath> <Pattern ignoreCase="true">/vehicles/{VIN}</Pattern> </URIPath>

<VariablePrefix>pathurireq</VariablePrefix>

<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>

</ExtractVariables>

Please correct me ,if I did anything wrong.

I don't know what you're doing wrong, but it works for me.

$ curl -i https://ORGNAME-ENVNAME.apigee.net/veeraraghavendra-1/vehicles/1276272
HTTP/1.1 200 OK
Date: Wed, 15 Feb 2017 21:47:45 GMT
Content-Type: text/xml
Content-Length: 65
Connection: keep-alive
Server: Apigee Router


<root><Tamatar>1276272</Tamatar><Aloo>1276272</Aloo></root>

See attached for a working API Proxy.

apiproxy-veeraraghavendra-1-20170215-135802.zip

ps: In the future, please be kind and format your XML when you paste it.

Many thanks @Dino,it is working now.

I can't download the zip file.