How to dynamically assign target.url to AppData.callbackURL in AssignMessage policy

Not applicable

Hi,

We were having a js policy but because it was not loading properly we have rewritten the assignmessage policy for the same .

But we are facing issue with setting target.url to AppData.callbackURL in the assignmessage policy.

The js file is as below

var callbackURL = context.getvariable("AppData.callbackURL");
context.setvariable("target.url", callbackURL);

The assignmessage rewritten for the above is

<AssignMesage name=.....>
<AssignTo type="request"/>
<Copy source="request">
  <Headers>
    <Header name="targetURL"/>
  </Headers>
</Copy>

<Add>
  <Headers>
    <Header name="targetURL">{target.url}</header>
  </Headers>

.....

As per the above code we are getting target.url as empty. I tried to use <Assignvariable> tag to assign target.url with ref as AppData.callbackURL but still not able to get the value in the request.

I am stuck here and not able to understand how can I go ahead with this.

Would appreciate for any help in this scenario.

Solved Solved
0 3 481
1 ACCEPTED SOLUTION

Hi, I'm sorry you're having trouble.

You can use JavaScript or the AssignMessge policy to assign a variable. Your JavaScript file looks just fine. It will actually work correctly. It will set the variable "target.url".

The AssignMessage policy you offered.... is not correct. If you simply want to assign a variable, then it would be something like this:

<AssignMessage name='AM-1'>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <AssignVariable>
    <Name>target.url</Name>
    <Ref>AppData.callbackURL</Ref>
  </AssignVariable>
</AssignMessage>

You observed that "it's not working" in both cases.

I don't know for sure what the problem is, but it is often common that when people try to use a policy to assign to the variable target.url, they attach the policy (JavaScript, or AssignMessage) in the proxy flow. That won't work.

At the inception of the target flow, the variable target.url is reset to the value specified in the TargetEndpoint. So if you attach the JavaScript policy in the proxy flow, this is what will happen:

  1. proxy flow starts
  2. at some point the policy executes, and target.url gets assigned
  3. target flow starts - this resets the value of target.url
  4. the change from point 2 is over-written

To avoid this behavior you need to attach the JavaScript policy in the Target flow.

The variable "target.url" is special in this regard. It is not the case that all variables are reset at the inception of the Target flow. But the target.url does get reset.

Therefore if you want to set the target url dynamically , you must do so with a policy that is attached in the target request flow.

Does this make sense?

View solution in original post

3 REPLIES 3

Hi, I'm sorry you're having trouble.

You can use JavaScript or the AssignMessge policy to assign a variable. Your JavaScript file looks just fine. It will actually work correctly. It will set the variable "target.url".

The AssignMessage policy you offered.... is not correct. If you simply want to assign a variable, then it would be something like this:

<AssignMessage name='AM-1'>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
  <AssignVariable>
    <Name>target.url</Name>
    <Ref>AppData.callbackURL</Ref>
  </AssignVariable>
</AssignMessage>

You observed that "it's not working" in both cases.

I don't know for sure what the problem is, but it is often common that when people try to use a policy to assign to the variable target.url, they attach the policy (JavaScript, or AssignMessage) in the proxy flow. That won't work.

At the inception of the target flow, the variable target.url is reset to the value specified in the TargetEndpoint. So if you attach the JavaScript policy in the proxy flow, this is what will happen:

  1. proxy flow starts
  2. at some point the policy executes, and target.url gets assigned
  3. target flow starts - this resets the value of target.url
  4. the change from point 2 is over-written

To avoid this behavior you need to attach the JavaScript policy in the Target flow.

The variable "target.url" is special in this regard. It is not the case that all variables are reset at the inception of the Target flow. But the target.url does get reset.

Therefore if you want to set the target url dynamically , you must do so with a policy that is attached in the target request flow.

Does this make sense?

Hi Dino,

Thanks a lot for the above explaination. I had earlier tried using the <Assignvariable> tag but was using braces in the Ref of {AppData.callbackURL} value which was why it was not assigning to the target.url

The policy is working as expected .I have removed the headers and only included the Assignvariable tags to assign the variables .

Right on. Glad to be of assistance, Sneha. I can understand your confusion around the {braces} and the Ref elements. Different policies in Apigee Edge use different conventions, and it can be confusing to know whether to use braces or something different.