KB Article #181037

How do you save information between API Manager policies within a single transaction?

Problem

The message whiteboard is reset between API Manager calls that are part of the same transaction, for example, between a custom routing policy and a response policy. How can data be persisted from one of the policies to another within a single call to API Manager?


Resolution

There's a special method on the message whiteboard that makes variables persist across the different API Manager policies within a single transaction. The Groovy script below can be called within the first policy, then the whiteboard information can be retrieved normally in later policies within the same transaction:


def invoke(msg) {
    msg.putGlobal("my.attribute", "some value")
    return true
}


Note: Despite the name of the method, this is different from making a variable truly global, as would happen to variables declared without 'def' or 'var'. True global variables are available across entirely different transactions and should not be used because they cause information leakage and race conditions. The putGlobal() method only makes the values available to separate API Manager calls within a single transaction.


Note: Make sure your attribute name is unique to your usage. I.e. Do not use putGlobal to override common attribute names, such as those used by API GW filters such as http.request.* attributes.