Reference information for accessing the context object
Example code snippets access the context object in user-defined policies.
You can reference context variables in user-defined policies with the
getContext()
and setContext()
functions and with JavaScript dot notation.
The context
object is like a whiteboard for storing transactional information.
Every policy in the same assembly gets the same context
object for a single
transaction. All of the policies can populate information into the context object and retrieve
information from it. Data is categorized based on JavaScript properties. For example, all of the request related data are stored under
request
and response
related data go to message
.
Pre-populated data might be read-only properties. Code that attempts to override read-only values
causes an exception.
Reading payload
Request and response payload are stored in a context object. Request payload is populated into
the request.body
and response payload is stored in the
message.body
. The data type of the request.body
and the
message.body
is determined by the value of
request.content-type
.
Here are examples that access the request payload.
Use dot notation to access the request.body
:
var requestPayload = context. request. body;
Use the get
function to get the request.body
:
var payload = context. get(' request.body ');
Writing payload
The message.body
starts with same value as the request.body
,
but the value can changed as the flow continues. After the policy implementation runs, the
message.body
becomes the payload for subsequent policies, if any, in the flow. The
following table shows the correlation between the message.body
data type and the
request.content-type
data type:
request.content-type | message.body |
---|---|
*/json | JSON |
*/+json | JSON |
text/* | String |
*/x-www-form-urlencoded | JSON (key or value represents the HTTP form data) |
*/xml | XML |
*/+xml | XML |
All others | Buffer |
Reading headers
Request and response headers are stored in the context
object. You can retrieve
the headers as in the following examples:
//get header
var requestHdr = context.get('request.headers.req-header-name');
var responseHdr = context.get('message.headers.resp-header-name');
Writing headers
Request headers are read-only headers, therefore, you cannot change them. However, you can modify
headers in message.headers
. The message.headers
are the headers
that are sent back to the client.
//set header
context.set('message.headers.x-custom-header', 'value');
context.message.headers.myheader ='myheader';
get()
function to retrieve it. Use
brackets, []
, to access this type of variable with dot-notation. For
example:
var contentType = context.message.headers['content-type'];
//or
contentType = context.get('message.content-type');
Reading context
Context is transactional-wide shared
variable space. For Micro Gateway
policies, a context object provides access to context variables. The get()
function
and dot-notation are used to retrieve context values.
For example:
var verb = context.get('request.verb');
var uri = context.get('request.uri');
var path = context.request.path;
where
context
object is passed to the policy handler function by default.Writing context
Similar to the get()
function that reads context variables, the set()
method writes context variables.
For example:
context.set('acme.itemnumber', 'item1');
JavaScript dot-notation also is supported, as in this
example:
context.acme.itemnumber2 = 'item2';