How to read the hostname of a Target Server in JS

I have configured a Target Server in my env.

However, I have  a  need to read the hostname configured for the target server in a JS.

I tried the below in Target Preflow, but is coming as empty

 

 

var tarHost=context.getVariable('target.host');

 

 

asaischintala_0-1629233539311.png

 

I've also configured the HTTPConnection node on target flow.

 

 

<HTTPTargetConnection>
        <LoadBalancer>
            <Server name="Backend1"/>
        </LoadBalancer>
        <Path>/api</Path>
</HTTPTargetConnection>

 

 

The routing seems to work as expected though.

Can you please let me know how to read the hostname of a specific Target Server in JS.
TIA.

@dchiesa1please advise.

Solved Solved
0 4 764
1 ACCEPTED SOLUTION

Why not use kvm approach and based of current flow name you are well aware  of the target ahead

Examples for a sample condition flow as shown below , in KVM you define the host

key as sample

value as https://learningapigee.com

<Condition>(proxy.pathsuffix MatchesPath "/sample") and (request.verb = "POST")</Condition>

In the flow you get the kvm as below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KV-Test" mapIdentifier="KVM_Test">
<DisplayName>KV-Test</DisplayName>
<ExpiryTimeInSecs>120</ExpiryTimeInSecs>
<Get assignTo="targetUrl">
<Key>
<Parameter ref="current.flow.name"/>
</Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>

Now you are aware of the target information ahead & can work on the logic

Finally you can assign the target as below instead of your current approach

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-TargetUrl">
<DisplayName>AM-TargetUrl</DisplayName>
<Properties/>
<AssignVariable>
<Name>target.url</Name>
<Template>{targetUrl}?{request.querystring}</Template>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

View solution in original post

4 REPLIES 4

The variable is not yet populated when you are in the Proxy Preflow. "target.host" would only be populated in a TargetEndpoint flow. 

Try moving to target response flow to make it work. Refer below --

The domain name of the target service returning the response to the API proxy.

https://docs.apigee.com/api-platform/reference/variables-reference#target

 

@API-Evangelist 
Looks like target.host is available only in Target Response Flow

Why not use kvm approach and based of current flow name you are well aware  of the target ahead

Examples for a sample condition flow as shown below , in KVM you define the host

key as sample

value as https://learningapigee.com

<Condition>(proxy.pathsuffix MatchesPath "/sample") and (request.verb = "POST")</Condition>

In the flow you get the kvm as below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KV-Test" mapIdentifier="KVM_Test">
<DisplayName>KV-Test</DisplayName>
<ExpiryTimeInSecs>120</ExpiryTimeInSecs>
<Get assignTo="targetUrl">
<Key>
<Parameter ref="current.flow.name"/>
</Key>
</Get>
<Scope>environment</Scope>
</KeyValueMapOperations>

Now you are aware of the target information ahead & can work on the logic

Finally you can assign the target as below instead of your current approach

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-TargetUrl">
<DisplayName>AM-TargetUrl</DisplayName>
<Properties/>
<AssignVariable>
<Name>target.url</Name>
<Template>{targetUrl}?{request.querystring}</Template>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>