Request/Response Variables/Serialization

Not applicable

Hello All,

According to the Apigee variables documentation, "request" and "response" should contain all of the matter in the request for a flow, and the response for a flow. In my JavaScript callout, I'd like to be able to use these variables so I can somehow log what's going in and out of our flows.

I have this written in my JavaScript policy:

print(context.getVariable("request")); context.proxyResponse.content = JSON.stringify(context.getVariable("request"));

In the Apigee console, I get:

com.apigee.flow.message.MessageImpl@2a3f3fa7

In the flow, I get a fault:

{"fault":{"detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"},"faultstring":"Execution of sup failed with error: Javascript runtime error: \"Access to Java class \"com.apigee.messaging.adaptors.http.message.HttpRequestMessage\" is prohibited. (sup_js#4)\""}}

To me, it appears that when calling this particular context variable, that is it coming back with a serialized version or something. Does anyone know how to actually expose or serialize (preferably to text or JSON) the internal data to this particular JavaScript variable?

I don't have any issues with using variables deeper than the root request (e.g. request.querystring, request.path, request.verb, etc.), but it would be nice to just get everything in one shot with one object, as opposed to Frankensteining pieces together for one encapsulated object.

TIA, Steve

2 8 2,835
8 REPLIES 8

adas
Participant V

@Stephan.Cossette If you are using javascript, instead of doing print(context.getVariable("request")) you can directly do print(request) or print(response). You could also do print(context.proxyRequest) or print(context.targetRequest) if you explicitly want to define it for the proxy or target scope.

The javascript object model documentation can be found here

Let me know if this helps. Happy to answer any further questions.

Not applicable

This is one of the most annoying things about trying to troubleshoot in Apigee. The other answer did not work, as the javascript policy failed to execute due to the variable not being defined.

Hi @Stephan.Cossette / @arghya das and @Timothy,

I came across a similar question and happen to land with this one.

As far as access the "request" as a whole, yes you can do that but it will be a Java object of type com.apigee.flow.message.Message.

If you are using context.getVariable("request") in JSC then you'll get reference of the above mentioined Java Object.

Although it is a little difficult to understand, javascript in Apigee is compiled to Java and run as java code.

So after getting the request object, you can use all the methods available in Message Object of apigee.flow.message.Message interface.

An example usage is like:

var req = context.getVariable("request");
//Print request body
print(req.getContent());


//print all headers names as java.util.Set
print(req.getHeaderNames());

//As javascript array of strings
var headerNames = req.getHeaderNames().toArray();

//Get all the query parameters names
print(req.getQueryParamNames())

It is unfortunate that the documentation is not that readily available for these classes.

May be @docs can see to that.

Cheers!!

Thanks, @Mohammed Zuber. Are you sure this works? Did you try it out? Doc team will investigate. Meantime, here's the Javadoc for classes: https://github.com/apigee/api-platform-samples/tree/master/docs/javadocs-javacallout

Hi @Floyd Jones,

I have tried it in 16.09 and 16.05 but it failed in 17.01, 17.05. Looks like the access has been restricted in the newer versions.

> Although it is a little difficult to understand, javascript in Apigee is compiled to Java and run as java code.

It is not difficult understand at all. That is the consequence of JavaScript being implemented as Rhino/Trireme in Edge.

The by-product of it is an ability to script Java and therefore to access all Java object underlying methods. See here. https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java

When I tried to run your snippet, I've got:


Execution of inspectmessage failed with error: Javascript runtime error: "Access to Java class "java.util.HashSet" is prohibited. (inspectmessage_js#13)"


where line 13 is: print(req.getQueryParamNames()).

An explanation and workaround for this is in:

https://community.apigee.com/questions/1858/why-am-i-getting-a-js-exception-when-accessing-one.html


Unfortunately, these suggestions do not work. When the javascript tries to execute, I get denied.

Execution of debugger failed with error: Javascript runtime error: "Access to Java class "java.util.TreeMap$KeySet" is prohibited. (debugger_js#13)"

Why would I be prohibited from seeing a Java class?

Not applicable

2 variables authentication keeps the product safe and to keep that data private. The recordings are clarifying great about this framework with customwritings com and I will download this product soon with this refreshed variant. Much obliged to you for offering this to us.