How to get all request header key value pairs for Logging?

How do I get all request header key value pairs in order to log?

On the documentation -> Apigee X Request Variables 

I see a way to get all request headers keys/names-> request.headers.names

How do I get all values for the corresponding names/keys ?

Please advise.

@dchiesa1 

Solved Solved
0 3 3,454
1 ACCEPTED SOLUTION

You could use a JavaScript step to iterate over the names. 

<Javascript name='JS-IterateHeaders' timeLimit='200' >
  <Source>
    var names = String(context.getVariable('request.headers.names'))
      .slice(1,-1)
      .split(',')
      .map(function(s) { return s.trim(); });
    names.forEach(function(name) {
      // do whatever you want here, eg add to a logging payload
      context.setVariable('DIAG-'+name, context.getVariable('request.header.' + name));
    });
  </Source>
</Javascript>

View solution in original post

3 REPLIES 3

In the same doc page that you shared you'll find request.header.header_name and request.header.header_name.
 values as options. Have you tried those yet?

So I just have to iterate over each header.name and get it's value?

Is there any other way than to manually iterate over the header name collection?

You could use a JavaScript step to iterate over the names. 

<Javascript name='JS-IterateHeaders' timeLimit='200' >
  <Source>
    var names = String(context.getVariable('request.headers.names'))
      .slice(1,-1)
      .split(',')
      .map(function(s) { return s.trim(); });
    names.forEach(function(name) {
      // do whatever you want here, eg add to a logging payload
      context.setVariable('DIAG-'+name, context.getVariable('request.header.' + name));
    });
  </Source>
</Javascript>