Using Javascript Extraction with Environment Config variables

rich
Participant I

Adding the Extract Variables and Javascript policy with the Target pre flow has works for changing the target url

context.setVariable("target.url",targetURL);

But, instead of the target URL, I will need to use a Environmental Variable for the base of my target servers (dev/test/prod)

How can I reference the Environmental Variable for a Target Server in the js resource context?

Solved Solved
0 6 622
1 ACCEPTED SOLUTION

What exactly do you mean by environment variable in the context of the api proxy?

You have two options:

1. Use target servers and you can define it separately for each environment

2. Use key value maps with environment scope, fetch the values from the KVM into a flow variable using the KVM operations policy. In the JS you can then access the variable in the usual format context.getVariable("prefix.varname")

View solution in original post

6 REPLIES 6

What exactly do you mean by environment variable in the context of the api proxy?

You have two options:

1. Use target servers and you can define it separately for each environment

2. Use key value maps with environment scope, fetch the values from the KVM into a flow variable using the KVM operations policy. In the JS you can then access the variable in the usual format context.getVariable("prefix.varname")

The second option may work since it is accessible from JS.

What I meant from environment variable was setting TargetServer for each environment.
Can I access the value of that from the javascript?

No, the Target server config values are not accessible as variables via JavaScript.

Why do you need to access the Target server configuration. Have you tried the <path> configuration in the httptargetconnection with a Target server configuration, won't that suffice your requirement?

I need the base path for the target specific to the chosen environment, in one form or another, I was suggesting Env. Config Target variables because those seem to be designed for this purpose,.

I still don't see a need to use javascript.

Target server configurations are environment specific. They will just have the host name (env specific)

The paths should be consistent irrespective of the environments, isn't it?

eg: target url for dev and qa would look like

https://dev.target.com/api/items

https://qa.target.com/api/items

you can have a target server with name myapi_target

In dev it will be configured with

host: dev.target.com; port: 443

In qa it would be configured as

host: qa.target.com; port 443

<HTTPTargetConnection>
<LoadBalancer>
<MaxFailures>0</MaxFailures>
<RetryEnabled>true</RetryEnabled>
<Server name="myapi_Target">
<IsEnabled>true</IsEnabled>
<IsFallback>false</IsFallback>
<Weight>1</Weight>
</Server>
<TargetDisableSecs>300</TargetDisableSecs>
</LoadBalancer>
<Path>api/items</Path> <!-- or path from a variable like pathsuffix etc -->
<SSLInfo>
<Ciphers/>
<ClientAuthEnabled>false</ClientAuthEnabled>
<Enabled>true</Enabled>
<IgnoreValidationErrors>false</IgnoreValidationErrors>
<Protocols/>
</SSLInfo>
<Properties>
</Properties>
</HTTPTargetConnection>

rich
Participant I

That is to say, I want to use an enviroment variable to establish my tartgetURL string

var baseTargetURL = *** some environental variable to target server ***