Stupidfrog
Stupidfrog

Reputation: 2102

how to read variable in assign message policy in APIGee

In Apigee

given a variable in javascript policy, example: var value = 123;

how to get this variable in the assign message policy?

by using {a} in the payload message is not reachable

Upvotes: 0

Views: 1639

Answers (3)

Komo
Komo

Reputation: 68

For the variable to be available for use in any Apigee policy, you first have to make it a flow variable. You can use Apigee's JavaScript Object Model method called context to to this (see ref here). In your JS policy, you have var {some-value} = 123;. To make this available as a flow variable,introduce this line in your JS policy after the variable assignment: context.setVariable("preferred-variable-name", some-value);

You can now access this preferred-variable-name in your AM policy as show below:

<AssignVariable>
    <Name>preferred-variable-name</Name>
    <Ref>preferred-variable-name</Ref>
</AssignVariable>

Just to mention it is possible to pass the value directly in the policy as well as shown below

<AssignVariable>
  <Name>variable_name</Name>
  <Value>123</Value>
</AssignVariable>

Read more here.

The <Name> tag can be set to any name.

Upvotes: 1

Pranav
Pranav

Reputation: 132

You can just set the context variable in jsPolicy like this:

context.setVariable("a","123");

You can use it after in any of the policies.

Ex :-

<QueryParams>
    <QueryParam name="a">{a}</QueryParam>
</QueryParams>

Upvotes: 1

Dolly Talreja
Dolly Talreja

Reputation: 91

You have to set the variable in the context object using context.setVariable("variable-name", value);

Refer to this variable in the assign_message policy using {variable-name}.

Upvotes: 1

Related Questions