Context variable in javascript showing up as 'Undefined'

hari_vr
Participant IV

Below is the request I am sending to my proxy :

curl
-X POST https://mydomain.com/path?scope=lol -H
'Content-Type: application/x-www-form-urlencoded' -d
'scope=openid%20accounts'

I am trying to access the scope in a javascript policy using the below expression:

context.proxyRequest.formParams['scope']

For some unknown reason, this does not work and I get the below error:

Javascript runtime error: "TypeError: Cannot read property "scope" from undefined. (ValidateRequest.js:5)"

I am able to use the other context variables like

context.proxyRequest.headers['Content-Type']
--> application/x-www-form-urlencoded
request.method
--> POST
context.proxyRequest.queryParams['scope']
-->lol

Can anyone shed some light on what might be going on with formparams and JS?

1 5 1,711
5 REPLIES 5

rmishra
Participant V

Could you try

context.getVariable('request.formparam.scope');

This works as expected. For the request

curl -X POST 
'https://host/path?scope=lol-queryparam' -H 'Content-Type: application/x-www-form-urlencoded'
-d scope=lol-formparam

we get

context.getVariable('request.formparam.scope'): lol-formparam

Looking into it, despite the documentation, this is not going to work:

context.proxyRequest.formParams['scope']

Also, for sure, the queryparams syntax is:

context.proxyRequest.queryParams['scope']

Note the camelCase queryParams, not lowercase queryparams.

Seems like a strange, longstanding doc bug. A discrepancy between the documented behavior and the actual implemented behavior. reference: b/88295772

Hi Dino, thanks for taking a look.

I used a bunch more print commands in the JavaScript and got these results :

For the request

curl -X POST 
 'https://host/path?scope=lol-queryparam'
 -H 'Content-Type: application/x-www-form-urlencoded' 
 -d scope=lol-formparam

we get

request.queryParams['scope'],camelcase: lol-queryparam
request.queryparams['scope'],no camelcase: lol-queryparam
request.headers['Content-Type']:application/x-www-form-urlencoded

So basically, whether we use the camelcase or not is irrelevant when it comes to queryParameters - it always works.

Also, whether we use the camel case or not is irrelevant when it comes to formParameters - it never works. This was confirmed by using the below print commands in the JS code.

print("request.formParams['scope'], camelcase:",request.formParams['scope']);
print("request.formparams['scope'], no camelcase:",request.formparams['scope']); 

using context.proxyRequest instead of request has no bearing on the responses, they remain the same.

So perhaps the documentation should be updated to state that queryParams is exchangable with queryparams (i.e. camel case check is not enforced).

As for form params, I think this is a bug and needs to be fixed. At the minimum, there should be a disclaimer in the documentation that form params are not accessbile from within the Javascript runtime using the 'context' object. Tagging @sgilson

Thanks, Hari, for the additional followup and information.

It seems you've solved your issue by using context.getVariable('request.formparam.foo').

If you have the desire, maybe you can try:

request.body.asForm['scope'] 

...but really, it's equivalent to the above.