Make Target Endpoint Dynamic based on Header Parameter

I have header parameter named 'tenant' and Target endpoint URL as 

<HTTPTargetConnection>
<URL>https://revenue-gateway-test.int.np.dal-2.abc.com/accounting/v1/invoiceRequest/summary</URL>
</HTTPTargetConnection>

I want to make the Target Endpoint basepath URL(revenue-gateway-test.int.np.dal-2.abc.com) dynamic based on Header parameter.

I do not want to make entire URL dynamic based on Header parameter

Let me know if that can done using Javascript

 

0 1 71
1 REPLY 1

Would suggest to use Assign Message in Target PreFlow instead of hard code URL approach.

Use KVM's - https://docs.apigee.com/api-platform/cache/key-value-maps  & read the target URL from it. Is the proxy basepath matching then you can append it else move it to kvm too & read it.

You can apply the condition(https://docs.apigee.com/api-platform/reference/conditions-reference) on the Assign Message itself to verify if the header exists and expect certain value..

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--Dynamic Target URL, value of private.targetUrl is fetched from encrypted KVM-->
<AssignMessage name="AM-TargetUrl">
  <DisplayName>AM-TargetUrl</DisplayName>
  <AssignVariable>
    <Name>target.url</Name>
    <Template>https://{private.targetUrl}{proxy.basepath}{proxy.pathsuffix}</Template>
  </AssignVariable>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</AssignMessage>


or, You can also do same using java script approach as well..This is for you to do by yourself to feel confident & we can do together..

1.Read the target basepath url from the kvm - var targetUrl = context.getVariable("private.targetUrl”);
//this is the host value from KVM - revenue-gateway-test.int.np.dal-2.abc.com
2.Read the path suffix var pathsuffix = context.getVariable("proxy.pathsuffix");
//assuming the incoming value is same as - /accounting/v1/invoiceRequest/summary

2.Read the incoming header value - var headerValue = context.getVariable("request.header.tenant")
3.Use if condition to check the header exists and not null & expect certain value then assign it to earlier read targetUrl value from kvm  

var targetUrl = "https://" + targetUrl + pathsuffix;
4. Finally assign to target.url
context.setVariable("target.url", targetUrl);
 
Make sure you wrap the whole code in try catch block for handling errors..

try {
   //CODE
} catch(error) {
        context.setVariable("debug", error);
        throw error;
}

 

Good luck.