Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

JS script reading all response headers and iterate over them afterwards

I'm implementing a JS script on apigee gateway, triggered via a JS-policy

I'm reading the response headers like this  

const responseHeaders = context.getVariable('response.headers.names');
 
It's returning this 

[Access-Control-Allow-Origin, Content-Encoding, Content-Type, Date, Strict-Transport-Security, Transfer-Encoding, Vary, x-ms-middleware-request-id]

So fare so good 🙂 

nest step is to iterate over this array, but I had not luck achieving this.  

I tried to iterate over this like this

responseHeaders.forEach(header => {
   print(headers);
   }); 
responseHeaders type of :[object JavaObject]

I investigate the type of the responseHeaders const and it returns the above

I suspect that the responseHeaders const is of a type not known in JS and therefore it's not handled as a array. 

Any suggestions to solved ? 

Solved Solved
0 6 285
1 ACCEPTED SOLUTION

This solved it

 

const responseHeaderNamesArray = responseHeaderNames.substring(1, responseHeaderNames.length - 1).split(',').map(h => h.trim());
// Remove brackets [], split string by comma, Trim whitespace

responseHeaderNamesArray.forEach(element => {
  print(element+": "+context.getVariable('response.header.'+element));
  // ... perform need operation for each response header
});

output is
Access-Control-Allow-Origin:
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Vary: Origin
...etc. 

 

 

View solution in original post

6 REPLIES 6

This solved it

 

const responseHeaderNamesArray = responseHeaderNames.substring(1, responseHeaderNames.length - 1).split(',').map(h => h.trim());
// Remove brackets [], split string by comma, Trim whitespace

responseHeaderNamesArray.forEach(element => {
  print(element+": "+context.getVariable('response.header.'+element));
  // ... perform need operation for each response header
});

output is
Access-Control-Allow-Origin:
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Vary: Origin
...etc. 

 

 

Your solution works well, but there's a more elegant way to achieve the same result by leveraging the native capabilities of the Java Set<String> object returned by context.getVariable("response.headers.names"). Since it stores a set, the best approach is to first convert it to an array and iterate over it. Here's an improved version:

 

var responseHeaders = context.getVariable("response.headers.names");
var headerArray = responseHeaders.toArray(); 

for (var i = 0; i < headerArray.length; i++) {
  var headerName = headerArray[i];
  var headerValue = context.getVariable("response.header." + headerName + ".values.string");
  print(headerName + ": " + headerValue);
}

 

This way, you avoid unnecessary string operations and take advantage of the native conversion, making the code cleaner and more efficient.

Hope this helps!

nmarkevich_0-1737397397342.png

 

 
 

 

 

Thank you both, @FrankHinrichsen and @nmarkevich, for sharing your expertise and providing solutions. Your contributions enrich our community and help others facing similar challenges.

Thanks @nmarkevich this looks better.... I kept the usage of forEach though :-). 

I had issues with this part.  

 

 var headerValue = context.getVariable("response.header." + headerName + ".values.string");

 

A little rewrite of my code, and it's now working fine

const responseHeaderNames = context.getVariable('response.headers.names').toArray();

responseHeaderNames.forEach(element => {
    var headerValue = context.getVariable('response.header.'+element);
    print(element + ": " + headerValue);
});

output 

Access-Control-Allow-Origin:
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Tue, 21 Jan 2025 07:28:21 GMT
..etc

how would you remove content.response header ? 

FYI this syntax can be used to remove variables

context.removeVariable('response.header.'+<insert header name>);

 

Top Solution Authors