Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to extract multiple variables from a multi value header?

Not applicable

I am using Apigee/ Edge extract variables policy.

Say the header looks like:

Cookie: theme=light; sessionToken=abc123

I tried the following in extract variable proxy but it doesn't seem to work.

<Header name="Cookie">
<Pattern ignoreCase="true">theme={theme}; sessionToken={token}</Pattern>
</Header>

Does the sequence of parameters (theme, sessionToken) matter? How about extra spaces, do I need to account for that?

Solved Solved
1 7 3,007
1 ACCEPTED SOLUTION

Hi @Gagan Arora, @David Allen's answer prints all headers. Here is the JavaScript code to parse the Cookie header into variables (like cookie.theme and cookie.sessionToken), using David's pattern:

var cookieHeaderStr = context.getVariable("request.header.Cookie");
cookieHeaderStr.split("; ").forEach(function(cookieStr) {
  if (cookieStr) {
    var sections = cookieStr.split("=");
    
    // set variable for each cookie
    context.setVariable("cookie." + sections[0], sections[1]);
  }
});

View solution in original post

7 REPLIES 7