Set target hostname to arbitrary value using a variable

I need to set the target endpoint hostname based on a variable, basically like this:

https://{customer_name}.mytarget.com

When I try to set it like this:

<AssignVariable>
  <Name>target.url</Name>
  <Value>https://{customer_name}.mytarget.com</Value>
</AssignVariable>

I get:

com.apigee.errors.http.server.ServiceUnavailableException

I have seen other suggestions to use the load balancing / TargetServers features, but this is not a solution for me. I cannot enumerate all possible customer name values in advance. customer_name is set based on the request and then included in the hostname.

Is there a way to set the target endpoint to an arbitrary hostname during runtime?

Solved Solved
0 4 1,368
1 ACCEPTED SOLUTION

yes, the AssignVariable is not doing what you want it to do. You must use the Template element, not the Value element. The Value element is not interpreted as a message template - it does not do variable replacement. As a result if you trace your request you will see that the actual hostname Apigee Edge is trying to use is

https://{customer_name}.mytarget.com

Yes, with curly braces and all. Obviously that is not a valid target URL.

What you want is:

<AssignVariable>
  <Name>target.url</Name>
  <Template>https://{customer_name}.mytarget.com</Template>
</AssignVariable>

View solution in original post

4 REPLIES 4

yes, the AssignVariable is not doing what you want it to do. You must use the Template element, not the Value element. The Value element is not interpreted as a message template - it does not do variable replacement. As a result if you trace your request you will see that the actual hostname Apigee Edge is trying to use is

https://{customer_name}.mytarget.com

Yes, with curly braces and all. Obviously that is not a valid target URL.

What you want is:

<AssignVariable>
  <Name>target.url</Name>
  <Template>https://{customer_name}.mytarget.com</Template>
</AssignVariable>

@Dino-at-GoogleInteresting. Does the assignVariable allow Template tag? I didn't see it as a child element in the docs though. It only shows the Name, Ref and Value tags.

Is this feature available only in the cloud? it worked on my apigee free org account. I tried the same on the on-prem version(4.18.05), I see the below error:

Error occurred while validation of bean Assign-Message-2.xml. Reason: - Schema validation failed. Cause : unexpected element (uri:"", local:"Template"). Expected elements are <{}Ref>,<{}Value>,<{}Name>. Line number : 7. Column number : 19. File name : Assign-Message-2.xml.

I am looking in the documentation here

https://docs.apigee.com/api-platform/reference/policies/assign-message-policy#assignvariable

...and I see the description for the Template element.

For now, the AssignVariable/Template element is available only in Apigee Edge cloud. I believe OPDK 19.01 will have this feature.

@Marlon Kautz, try using the javascript policy in the target request flow to set it. It works.

var customerName = context.getVariable("customer_name");
context.setVariable("target.url", "https://"+customerName+".mytarget.com")