How to set target url with a KeyValueMap property

Not applicable

I would like to dynamically set the target url to values from the KVM (to configure url that differ between test and prod environments)

I tried to get it that way but apparently it wrong:

    <HTTPTargetConnection>
        <URL>{configs.payments-storage-service.targetUrl}</URL>
    </HTTPTargetConnection>

I can not use TargetServers configuration as it is not accessible in my JS scripts files

Solved Solved
0 11 4,285
1 ACCEPTED SOLUTION

Hi @YuriAbaev,

You have to use Javascript callout in target pre-flow to build target URL all including query parameters as they will not be passed automatically.

Use this code for setting target URL.

 var targetUrl = context.getVariable("configs.payments-storage-service.targetUrl");
 //get the path suffix
 var pathsuffix = context.getVariable("proxy.pathsuffix");
 
//get Query string 
var queryString = context.getVariable("request.querystring");
context.setVariable("target.url", targetUrl + pathsuffix + queryString );

Also, the approach described above will not work with HTTPTargetConnection in TargetEndpoint configuration. That approach works only in Service Callouts.

Hope this helps!

View solution in original post

11 REPLIES 11

Hi @YuriAbaev,

You can set a flow variable "target.url" in target preflow for dynamically changing the target URL. This will be accessible in any of the target flows.

Cheers!

@Mohammed Zuber Before calling target I need to make some Service callouts to other services (who's urls are changing between environments as well) should I override target.url before each outbound call I make from the proxy?

@YuriAbaev The value set in target.url is for the final backend call from the TargetEndpoint. ServiceCallout targets can be set as described by @Sai Saran Vaidyanathan below.

Hi @YuriAbaev

Were you able to get the values from the KVM ? If you did make sure you don't add the protocol. You can keep the rest in KVM. For example, if your target URL is http://httpbin.org/get, have httpbin.org/get in the KVM and in the Target Configuration

<HTTPTargetConnection>
	<URL>http://{configs.payments-storage-service.targetUrl}</URL>
</HTTPTargetConnection>

Assuming configs.payments-storage-service.targetUrl is getting picked from KVM and has the value httpbin.org/get

Let me know if this works

@Sai Saran Vaidyanathan

Thanks for your kind reply,it still does not work as I still get :

"errorcode": "messaging.adaptors.http.flow.ServiceUnavailable"

I use it in the Target Endpoint.

when I replace "{configs.payments-storage-service.targetUrl}" with the real url it works fine.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndpoint name="payments">
    <Description/>
    <FaultRules>
        <FaultRule name="create-payment-rule">
            <!--<Step>-->
            <!--    <Name>Extract-Variables-Create-Payment-Error</Name>-->
            <!--</Step>-->
            <Step>
                <Name>EV-CreatePaymentError</Name>
            </Step>
            <Step>
                <Name>AM-BadRequestError</Name>
                <Condition>(response.status.code == "400" ) </Condition>
            </Step>
            <Step>
                <Name>AM-GeneralError</Name>
                <Condition>(response.status.code == "500") </Condition>
            </Step>
        </FaultRule>
    </FaultRules>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>KVM-GetPaymentStorageStorageConfigs</Name>
            </Step>
            <Step>
                <Name>Set-X-Payment-Service-Api-Version</Name>
            </Step>
            <Step>
                <Name>Set-X-Request-ID</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <Flows/>
    <HTTPTargetConnection>
        <URL>http://{configs.payments-storage-service.targetUrl}</URL>
    </HTTPTargetConnection>
</TargetEndpoint>

Hi @YuriAbaev - Can you make sure the value is being picked from KVM ? Can you find it on trace ? If not, can you add a Javascript policy after the KVM policy to print the variable ?

Also can you please paste the KVM policy code and confirm that the KVM you are trying to get is configured in the correct environment ?

@Sai Saran Vaidyanathan I can get the KVM value with js policy



thats theconfiguration

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<KeyValueMapOperations async="false" continueOnError="false" enabled="true" name="KVM-GetPaymentStorageStorageConfigs" mapIdentifier="payments-storage-service">
    <DisplayName>KVM-GetPaymentStorageStorageConfigs</DisplayName>
    <Get assignTo="configs.payments-storage-service.targetUrl" index="1">
        <Key>
            <Parameter>targetUrl</Parameter>
        </Key>
    </Get>
    <Get assignTo="configs.payments-storage-service.api-version" index="1">
        <Key>
            <Parameter>api-version</Parameter>
        </Key>
    </Get>
    <Scope>environment</Scope>
</KeyValueMapOperations>

Hi @YuriAbaev - Looks like you will have to set the target.url via JS (or Assign Message) as mentioned by @Mohammed Zuber.

Hi @YuriAbaev,

You have to use Javascript callout in target pre-flow to build target URL all including query parameters as they will not be passed automatically.

Use this code for setting target URL.

 var targetUrl = context.getVariable("configs.payments-storage-service.targetUrl");
 //get the path suffix
 var pathsuffix = context.getVariable("proxy.pathsuffix");
 
//get Query string 
var queryString = context.getVariable("request.querystring");
context.setVariable("target.url", targetUrl + pathsuffix + queryString );

Also, the approach described above will not work with HTTPTargetConnection in TargetEndpoint configuration. That approach works only in Service Callouts.

Hope this helps!

@Mohammed Zuber thanks alot!

Yes @YuriAbaev,

Need to set the variable target.url using assign message policy or javascript policy.

Ensure the variable configs.payments-storage-service.targetUrl has the valid url value in trace.

target.urlScope begins: Target request

Type: String

Permission: Read/Write

The URL configured in the TargetEndpoint XML file or the dynamic target URL (if target.url is set during the message flow). The variable does not include any additional path elements or query parameters. Returns null if called out of scope or otherwise unset.

Use the following Javascript Policy in Target EndPoint -

var target_url=context.getVariable("configs.payments-storage-service.targetUrl");
//print the value for a check
print(target_url);
context.setVariable("target.url",target_url);

OR

Use the following Assign Message Policy in Target EndPoint -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Accept-to-Json">
    <DisplayName>Assign Accept to Json</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>target.url</Name>
        <Ref>configs.payments-storage-service.targetUrl</Ref>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Keep us posted, thank you!