Search This Blog

Showing posts with label params. Show all posts
Showing posts with label params. Show all posts

Tuesday, 7 May 2013

Useable Javascript Variables - all what you need !

On my way to find out how to manipulate flows and request attributes with javascipt I found out that there are some variables I can use and others are not useable - not documented in this way!

This is a reference on what you can use...


First have a look at the variable reference and tell me if you understand it....and have always a look at the permission column - this is really important!
http://apigee.com/docs/enterprise/content/predefined-variables

As you can see most variables are read-only and some are writable.

We start with "request.body" - can you find it?

Ah - sorry - look here for manipulating with javascript:

http://apigee.com/docs/enterprise/content/scripting-api-flow

Found it? No? Try here:

http://apigee.com/docs/enterprise/content/policies/add-scripting-features-to-your-API-using-JavaScript

or here looks interesting with more variables:

http://apigee.com/docs/api-platform/content/api-proxy-configuration-reference

and last hope is perhaps here:

http://apigee.com/docs/enterprise/content/apigee-javascript-object-model


A lot of reference - and believe me, because I tried the most - useless !

Have a look at this - here we find "request.body" - I will explain later what we need to know about this.
http://apigee.com/docs/enterprise/api/apigee-javascript-object-model


Enough! If you can't win - confuse ! Forget everything....


Use this on a request flow:   e.g. proxyendpoint/?test=123

QUERY-PARAMETER
var myvar = request.queryParams.test;  // myvar=123 - get a query parameter
request.queryParams["whatever"]="nice"; // set a queryparameter

HEADER
request.headers['Content-Type'] = 'application/xml'; // set Content-Type
request.headers['Content-Type'] = 'application/json'; // set Content-Type
request.headers['Content-Type'] = 'text/html'; // set Content-Type
request.headers['Newtest']="something"; // set Header-Var - good for debugging !!!

REQUEST-METHOD
request.method = 'GET';      // set request-method - use it to manipulate incoming requests-methods
request.method = 'POST';    // set request-method - use it to manipulate incoming requests-methods
request.method = 'PUT';     // set request-method - use it to manipulate incoming requests-methods
request.method = 'DELETE'; // set request-method - use it to manipulate incoming requests-methods

BODY / PAYLOAD / CONTENT
var oldcontent=request.body.asJSON; // get content as JSON ...if JSON
var oldcontent=request.body.asXML; // get content as XML...if XML

request.body = myrequest;          //    set a new body of reqest aka message aka payload


VARIABLES
(with context you can use it on request or response)
var newvar=context.getVariable("path.uri"); // get variables from variable reference(first link on this page) or variables exctracted from ExtractVariables-Policy!

context.setVariable("target.url",newurl); // to dynamically set a target-endpoint - can be with queryparams

context.setVariable("importantthing","test123"); // with setVariable you can also set own Variables and fetch them in later steps !!! Important !

important variables in combination with set/getVariable
target.url => target endpoint url - could be with queryparams on set or get
proxy.pathsuffix => the path after the basepath - Read-ONLY !!!


Use this on a response flow:

BODY / PAYLOAD / CONTENT
var responsecontent=response.content.asJSON; // if response is JSON - get the response
var responsecontent=response.content.asXML; // if response is XML - get the response
// do something with the response -> I will write another post for easy manipulation
response.content=JSON.stringify(data); // data is a javascript-object -> result is JSON

HEADER
response.headers['Content-Type']='application/json'; // set new content-type

some more will follow....most times nearly the same like request.....


If you found something important I missed - please test it first and then write a comment!