Switch statement doesn't seem to work in Javascript Policy

I have a Javascript policy with the following code:

var scenario = context.proxyRequest.queryParams['scenario'];
switch (scenario) {
case "abc":
  print("Case is abc");
  break;
case "xyz":
  print("Case is xyz");
  break;
default:
  print("No Match: The value of scenario is: " + scenario);
}

My URL is https://abc.com/test?scenario=abc

I am finding that the code falls into the "Default" path and it prints:

No Match: The value of scenario is: abc

so it confirms that the query parameter was properly extracted and put into the variable scenario.

If so then why doesn't the case: "abc" path get executed? Why does it take the default path?

I hardcoded the value of variable scenario as follows:

var scenario = "abc"

In this case it does go to the case" "abc" path.

Why does it work in the latter case but not in the former case?

Solved Solved
0 2 470
1 ACCEPTED SOLUTION

Hi @prabhup1

Can you change code from

var scenario = context.proxyRequest.queryParams['scenario'];

to

var scenario = context.getVariable("request.queryparam.scenario");

It should work. Please refer to this link

View solution in original post

2 REPLIES 2

Hi @prabhup1

Can you change code from

var scenario = context.proxyRequest.queryParams['scenario'];

to

var scenario = context.getVariable("request.queryparam.scenario");

It should work. Please refer to this link

@Sai Saran Vaidyanathan

Hello Sai,

It worked. Thank you.

I am curious though.

Why didn't the Switch statement work with the following var assignment even though the variable "scenario" had the correct value as confirmed by the Print statement?

var scenario = context.proxyRequest.queryParams['scenario'];