Search This Blog

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!


7 comments:

  1. How to set request.body/request.content to an object like {a:{b:c}} in a js policy? Thank you.

    ReplyDelete
  2. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Apigee, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Apigee. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com



    ReplyDelete
  3. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Apigee , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Sangita Mohanty
    MaxMunus
    E-mail: sangita@maxmunus.com
    Skype id: training_maxmunus
    Ph:(0) 9738075708 / 080 - 41103383
    http://www.maxmunus.com/


    ReplyDelete
  4. I like your blog a lot. Its informative and full of information. Thank you for sharing.
    Melbourne Web Developer

    ReplyDelete
  5. Its a wonderful post and very helpful, thanks for all this information.
    Javascript Training institute in Noida

    ReplyDelete
  6. I would like to say thank you for this special website. . keep writing such an informative blog. The visa for Turkey is completely open and you Explore the Turkey like a local . The Hagia Sophia (Aya Sofya) Mosque is the most likely and visited palace in Turkey according to a report.

    ReplyDelete