How to extract target url from service callout policy?

My servicecallout policy name and display name is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="SC-Test1" enabled="true" continueOnError="false" async="false">
    <DisplayName>SC-Test1</DisplayName>

Currently, I am setting the URL as follows:

<HTTPTargetConnection>
        <Properties/>
        <URL>https://something.com/path?queryparameter=queryvalue</URL>
</HTTPTargetConnection>

Based on documentation, I see that the proper way is to do “servicecallout. {policy-name}.target.url”. However, when trying to put a JS policy after my callout:

print(context.getVariable("servicecallout.SC-Test1.target.url"));

It returns null.

From the trace, I can clearly see that on the SC policy under properties, the full target URL I need is provided within the property “ServiceCallout.requesturl”. However, I can’t find a way to extract this property.

The closest I’ve been able to get is “servicecallout.requesturi” which returns “/path?queryparameter=queryvalue”. However, this format isn’t consistent as servicecallout.requesturl and servicecallout.targeturl both return null.

3 Likes

Thanks for the question. Very clear.

I think the reason you cannot get the information you want from “servicecallout.SC-Test1.target.url” is that it was implemented as an input variable, not an output variable.

Let me check to see if there is an output variable that would provide exactly the information you seek.

Did you try reading “servicecallout.requesturi” ? (ps: case matters)

If that did not satisfy, then I think there is no variable that allows you to READ the URL that is used for the ServiceCallout.

And I think there ought to be a variable that allows that.

I’ll log a feature request. For now you’ll have to work around this behavior. One way to do that is to configure the SC policy to use a variable in the URL.

<HTTPTargetConnection>
        <Properties/>
        <URL>{my_sc_url}</URL>
</HTTPTargetConnection>

That might not exactly work. You might need to do this:

<HTTPTargetConnection>
        <Properties/>
        <URL>https://{my_sc_host}{my_sc_path}</URL>
</HTTPTargetConnection>

And… THEN read servicecallout.POLICYNAME.target.url

I have one followup question though: why do you need to read the URL in the proxy flow? The URL is hard-coded there in the element.

Thanks for commenting, Dino. Essentially, I am using a complex shared flow operates logic on a set of information (service call out target URL being one of them). Hence, I need to make it generalized such that I can extract it as a variable rather hard-coding. The case above where I hard-code the URL is really for testing purposes. However in production we are expected to avoid the hard-coding and the target URL will actually be invisible to us.

OK I understand. And can you try it that way? Can you try it, with the URL “not hard coded”? The code path in the gateway is different, depending on whether you use a hardcoded url or a message template as

<URL>https://{sc_target_host}{sc_target_path}</URL>

When you do that ^^, the target.url variable is written, and you can later read it. I believe that may solve your problem.

You can get the target url as below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>Service Callout-1</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myreq">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>calloutResponse</Response>
    <HTTPTargetConnection>
        <Properties>
            <Property name="io.timeout.millis">20000</Property>
        </Properties>
        <URL>https://api.github.com/users</URL>
    </HTTPTargetConnection>
</ServiceCallout>

myreq.url = https://api.github.com/users

print(context.getVariable("myreq.url"));