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

Unable to fetch uri variable from request url using Extract variable or assign message policy

Not applicable

My requirement is quite simple.I am sending a request url 'http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint' which is my proxy endpoint url .I will add one variable to request url 'http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/13' ,the value '13' needs to picked up by 'extractvariable' or 'assignmessage' policy and prepare target request based this value.Desired target url is 'http://www.thomas-bayer.com/sqlrest/CUSTOMER/13'.

I have used below policies at the Target endpoint request flow and tried to call backend .My requirement is clear ,but I am not able to create complete code for this .

Here is the desinged code.

API proxy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <APIProxy revision="1" name="ConditionalRoutingwithDynamicEndpoint"> <Basepaths>/conditionalroutingwithdynamicendpoint</Basepaths> <ConfigurationVersion majorVersion="4" minorVersion="0"/> <CreatedAt>1477473512324</CreatedAt> <CreatedBy>nvraghavendra254@gmail.com</CreatedBy> <Description/> <DisplayName>ConditionalRoutingwithDynamicEndpoint</DisplayName> <LastModifiedAt>1477487807086</LastModifiedAt> <LastModifiedBy>nvraghavendra254@gmail.com</LastModifiedBy> <Policies> <Policy>Assign-Message-1</Policy> <Policy>GetZipCode</Policy> </Policies> <ProxyEndpoints> <ProxyEndpoint>default</ProxyEndpoint> </ProxyEndpoints> <Resources> <Resource>jsc://JavaScript-1.js</Resource> </Resources> <Spec/> <TargetServers/> <TargetEndpoints> <TargetEndpoint>default</TargetEndpoint> </TargetEndpoints> <validate>false</validate> </APIProxy>

Extract variable:GetZipCode

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ExtractVariables async="false" continueOnError="true" enabled="true" name="GetZipCode"> <DisplayName>GetZipCode</DisplayName> <!--<Pattern ignoreCase="true">/a/{pathSeg}</Pattern>--> <URIPath> <Pattern ignoreCase="true">/{pathSeg}</Pattern> </URIPath> <!-- Uncomment for extraction from header --> <!--<Header name="ZIP">--> <!-- <Pattern ignoreCase="true">{zipcode}</Pattern>--> <!--</Header>--> <Source>request</Source> <VariablePrefix>getcityforecastbyzip</VariablePrefix> </ExtractVariables>

Assign message:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1"> <DisplayName>Assign Message-1</DisplayName> <Properties/> <Copy source="request"> <Headers/> <QueryParams/> <FormParams/> <Payload/> <Verb/> <StatusCode/> <ReasonPhrase/> <Path/> </Copy> <AssignVariable> <Name>target.url</Name> <Value>http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id}</Value> </AssignVariable> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> <AssignTo createNew="false" transport="http" type="request"/> </AssignMessage>

My implementation might be wrong ,please suggest me the appropriate way to acheive my requirement.

Solved Solved
0 5 1,485
1 ACCEPTED SOLUTION

Hi @veeraraghavendra,

  • Scenario 1 - Assuming the base path be '/'
<Pattern ignoreCase="true">/conditionalroutingwithdynamicendpoint/{zipcode}</Pattern>

  • Scenario 2 - Assuming the base path be '/conditionalroutingwithdynamicendpoint'
<Pattern ignoreCase="true">/{zipcode}</Pattern>

Extract Variable Policy - Assuming Scenario 2 - the uri param is extracted to a flow variable called zipcode

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="true" enabled="true" name="GetZipCode"> 
<DisplayName>GetZipCode</DisplayName>
<URIPath name="zipcode">
<Pattern ignoreCase="true">/{zipcode}</Pattern> 

</URIPath> 

<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> 

</ExtractVariables>


Javascript Policy - Assign target.url

//get zipcode value already extracted
var zipcode=context.getVariable("zipcode");

//frame target_url
var target_url="http://www.thomas-bayer.com/sqlrest/CUSTOMER/"+zipcode;

//assign to target.url flow variable
context.setVariable("target.url",target_url);

Following is the issue with Assign Message Policy you have used -

<Ref> tag is used to specify the source variable.

http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id} as a whole,having not found as a variable fails to assign the value.

<Value> tag is used to specify the value directly, no substitution occurs here. Hence, http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id} is directly set as target.url and {id} is not replaced

<AssignVariable>
<Name>target.url</Name>
<Value>http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id}</Value> 
<Ref>http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id}</Ref>
</AssignVariable>

Hope this works, thank you!

View solution in original post

5 REPLIES 5

Hi @veeraraghavendra,

  • Scenario 1 - Assuming the base path be '/'
<Pattern ignoreCase="true">/conditionalroutingwithdynamicendpoint/{zipcode}</Pattern>

  • Scenario 2 - Assuming the base path be '/conditionalroutingwithdynamicendpoint'
<Pattern ignoreCase="true">/{zipcode}</Pattern>

Extract Variable Policy - Assuming Scenario 2 - the uri param is extracted to a flow variable called zipcode

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="true" enabled="true" name="GetZipCode"> 
<DisplayName>GetZipCode</DisplayName>
<URIPath name="zipcode">
<Pattern ignoreCase="true">/{zipcode}</Pattern> 

</URIPath> 

<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> 

</ExtractVariables>


Javascript Policy - Assign target.url

//get zipcode value already extracted
var zipcode=context.getVariable("zipcode");

//frame target_url
var target_url="http://www.thomas-bayer.com/sqlrest/CUSTOMER/"+zipcode;

//assign to target.url flow variable
context.setVariable("target.url",target_url);

Following is the issue with Assign Message Policy you have used -

<Ref> tag is used to specify the source variable.

http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id} as a whole,having not found as a variable fails to assign the value.

<Value> tag is used to specify the value directly, no substitution occurs here. Hence, http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id} is directly set as target.url and {id} is not replaced

<AssignVariable>
<Name>target.url</Name>
<Value>http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id}</Value> 
<Ref>http://apitechbasics-test.apigee.net/conditionalroutingwithdynamicendpoint/{id}</Ref>
</AssignVariable>

Hope this works, thank you!

Thanks for your help,let me try this way

Hi Nisha,

Many thanks for helping me on this .It is working now ,

Thanks Again

@Nisha Mallesh , Great answer, Just FYI, use code button in editor to beautify code instead of table border.

@Anil Sagar Thanks, Yes I have started using them, in my latest posts.

Top Solution Authors