How to test request header existence?

Not applicable

I try to use a javascript policy to test existence of a request header. Here is my code:

var auth = request.headers.authorization;
print('typeof authorization header: ' + (typeof auth));
print('authorization header === null? ' + (auth === null));
print('authorization header === undefined? ' + (auth === undefined));
print("authorization header == 'undefined'? " + (auth == 'undefined'));
print('print authorization header: ' + auth);

If my request contains an Authorization header, the policy prints the following output:

typeof authorization header: object
authorization header === null? false
authorization header === undefined? false
authorization header == 'undefined'? false
print authorization header: fdafdafdsafdsa

However, if the request doesn't contain an Authorization header, the policy prints the following output:

typeof authorization header: object
authorization header === null? false
authorization header === undefined? false
authorization header == 'undefined'? false
print authorization header: undefined

Except the difference on last print out, the tests on null or undefined are same. The last print out says the value is undefined, even though tests above against undefined are false.

What data structure is used for non-exist http header? Any alternative that I can use to test the existence of http header?

0 1 2,763
1 REPLY 1

Not applicable

context.proxyRequest.headers should give you the map of all the headers.

You should be able to use a sample code like this to check if a header is present in the request

var headers = context.proxyRequest.headers;
context.setVariable('all_headers', headers);


if("foo" in headers){
    context.setVariable('is_foo_present', "yes");
	
}else{
    context.setVariable('is_foo_present', "no");
	
}