Javascript Error

I am trying to transform the response using JS policy. For some of the responses, I am getting the following error. When i debugged , i could see for the responses that get the following error, some values are not present. Hence the corresponding variable in JS is getting populated as "undefined" causing this error. When I comment out that variable, it works fine.

{"fault":{"faultstring":"Execution of BuildGoogleresponse failed with error: Javascript runtime error: \"Access to Java class \"java.lang.Class\" is prohibited. (BuildGoogleresponse_js#62)\"","detail":{"errorcode":"steps.javascript.ScriptExecutionFailed"}}}

Questions:

1)How do i dynamically verify that a value exists for a variable and populate it in the proxy response.

2)Why cant i use a jsonpath like below in the policies.Javascript shows error and if i use it in ExtractVariable policy, I dont see any value getting populated in the variables. This jsonpath works well on website jsonpath.org.

$.photos[?(@.metadata.source.type='PROFILE')].url

I have attached the javascript for your reference.

Regards,

Rajeev S


0 1 2,058
1 REPLY 1

Hi, Rajeev. You wrote:

"How do i dynamically verify that a value exists for a variable and populate it in the proxy response."

I don't know exactly what you mean, but I'll give some information and maybe it will help. I think there are two different parts there. First, let me answer the question "how can I verify that a value exists for a variable?" Some code that works in a JS callout for this purpose, is like this:

var value = context.getVariable('variable_name_here');
if (value) {
    context.setVariable('response.content', 'the value exists. ['+value+']');
}
else {
    context.setVariable('response.content', 'the value DOES NOT exist');
}

If you run this as a JS policy in the response flow, then you will see the appropriate response. (this policy must run in the response flow in order to be able to successfully set response.content. )

To demonstrate this more effectively, maybe try the same code but with two different variables, one that you know is set, and one that you know is not set. like this:

function getDiagnostics(varname) {
  var value = context.getVariable(varname);
  if (value) {
      return '"' + varname + '"=['+ value + ']';
  }
  return '"' + varname + '" has no value.';
}

var allDiags = ['this_variable_is_not_set', 'request.verb']
  .map(getDiagnostics)
  .join('\n');
  
context.setVariable('response.content', allDiags);

The output you see from such an API proxy will be like this:

"this_variable_is_not_set" has no value.
"request.verb"=[GET]

Ok, part 2. "How do I .... populate it in the proxy response?"

And the answer to that is: You can use either AssignMessage or you can use JavaScript.

If this does not answer your question, can you be more specific about what you tried, the results you saw, and the results you expected to see?

The jsonpath question is a separate question entirely. It seems to be completely unrelated to this question about JavaScript and testing values of variables. I encourage you to ask the jsonpath question in a new, separate question. And there again, specify

  1. what specifically did you try?
  2. what results did you see?
  3. what results did you expect to see?

Also, I'm not sure I'm actually answering your REAL question because you started your question with

"I am trying to transform the response using JS policy."

But your question did not provide any details as to what you were trying to transform, and how. If you have a question on that, please ask it separately.

4114-ask-a-question-2.png