# How to handle multi-value headers in Javascript?

**URL:** <https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458>\
**Category:** Knowledge Hub\
**Tags:** apigee\
**Created:** [March 31, 2015, 3:18pm UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458 "2015-03-31T15:18:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [March 31, 2015, 3:18pm UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/1 "2015-03-31T15:18:25Z")

</div>

Updated with **_New Information_**!

The Javascript callout capability in Apigee Edge is super flexible, making it nice for doing just about anything to a request or response message, whether that is parsing it, extracting data, transforming it, searching, validating, setting default values, and so on. Just running through that list makes me think I could post a series of quick articles here on the community site, outlining how to do each of these things. If I were to do that, I think [@Birute Awasthi](https://community.apigee.com/users/752/birute.html) here at Apigee would be very pleased (She’s the community advocate). I’ll make no commitments, because I have a day job. But I will write at least ONE article in the set, on _this_ topic: handling multi-value headers.

RFC2616, the [IETF RFC that defines the HTTP protocol](http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2) says:

> Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one “field-name: field-value” pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.

Clearly, it is legal for an HTTP request or response to have multiple values for a single header, or multiple instances of the same “header key”. A good example might be, Cookie headers. A request may include multiple Cookie headers, something like:

```auto
 GET /alpha/beta?q=7
 Host: [www.example.com](www.example.com)
 Cookie: JSESSIONID=alphabeta120394049
 Cookie: AWSELBID=baaadbeef6767676767690220

```

And a response might include multiple Set-Cookies:

```auto
 HTTP/1.1 200 OK
 Server: Apigee-Edge
 Set-Cookie: JSESSIONID=alphabeta120394049; HttpOnly
 Set-Cookie: AWSELBID=baaadbeef6767676767690220; Path=/alpha

```

So how would a developer access those headers from within a Javascript callout? The “normal” way to access a single-valued header is:

```auto
var hdr = context.getVariable('request.header.host'), 
    ctype = context.getVariable('request.header.content-type);

```

Accessing a multi-valued header in the same way returns a Java ArrayList to the Javascript. (Remember, the JS callout is running inside the Rhino script engine, which itself is hosted on the Java VM. So the values returned from such calls as context.getVariable() are actually set by Java code).

According to [the doc page on variables in Apigee Edge](http://apigee.com/docs/api-services/reference/variables-reference) , multi-valued headers can be accessed with a .values suffix. In Javascript, this would look like this:

```auto
 var hdr = context.getVariable('response.header.set-cookie.values');

```

If you then try to request hdr.toString(), you may get this error:

```auto
{
  "fault": {
    "faultstring": "Execution of Javascript-ExtractResponseCookies failed with error: Javascript runtime error: \"Access to Java class \"java.util.ArrayList\" is prohibited. (extractResponseCookies_js#11)\"",
    "detail": {
      "errorcode": "steps.javascript.ScriptExecutionFailed"
    }
  }
}

```

VERY unfriendly. In my opinion, this shouldn’t happen, and I’m petitioning the product gods to change this behavior.

The good news is there is a way to avoid this.

UPDATE, 2019 Feb 26.

If you want to get the raw, string value of a header, that may or may not have commas within it, then you can use this form:

```auto
 var hdr = context.getVariable('response.header.set-cookie.values.string');

```

Just append “.values.string” to the “request.header.HEADERNAME” . This works for any header, request or response. For example, suppose a request header called “foo” which has a value “a=bar,b-123”. Retrieving “request.header.foo.values” gives you the ArrayList thing I described above. Retrieving “request.header.foo.values.string” gives you the original string value of the header, “a=bar,b=123”.

This also works in Condition statements (outside of JavaScript):

```auto
   <Step>
     <Name>SC-CheckAuthorization</Name>
     <Condition>request.header.foo.values.string = "a=bar,b=123"</Condition>
   </Step>

```

* * *

This is the older workaround I described previously (Don’t Use This, it’s too complicated and has no advantage over the previous method):

```auto
var hdr = context.getVariable('response.header.set-cookie.values')+'';

```

This coerces the value, which is an arraylist, to a string. The result looks something like “[value1, value2, value3]”, so you need to deal with those square brackets if you decide to parse the value. For example:

```auto
// get the array of header values: 
var a = hdr.substring(1, hdr.length-1).split(',');

```

Pretty simple, eh? Thanks to [@Mike Dunker](https://community.apigee.com/users/89/mdunkerapigeecom.html) for the tip. To carry the example just a little further, if you want just the first portion of each cookie (not the HttpOnly, nor the Path, nor the Expires, etc), then:

```auto
for (var i=0, L = a.length, oneHeader; i<L; i++) {
  oneHeader = a[i].split(';')[0].trim();
}

```

Ok, I hope this was helpful. In my next installment I will outline how to automatically file Birute’s nag emails into a special invisible email folder.

---

<div class="post-metadata">

**Author:** ![birute](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@birute](https://discuss.google.dev/u/birute)\
**Post date:** [March 31, 2015, 4:58pm UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/2 "2015-03-31T16:58:07Z")

</div>

[@Dino](https://community.apigee.com/users/803/dchiesa.html) definitely! and I think many people hanging out here would appreciate it.

Make us all proud 🙂

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [May 1, 2015, 1:21am UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/3 "2015-05-01T01:21:24Z")

</div>

Thanks, [@Dino](https://community.apigee.com/users/803/dchiesa.html). I’ve just wanted to let you know that I helped another developer with this solution.

---

<div class="post-metadata">

**Author:** ![API-Evangelist](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/api-evangelist/32/7651_2.png) [@API-Evangelist](https://discuss.google.dev/u/API-Evangelist)\
**Post date:** [April 7, 2016, 3:22pm UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/4 "2016-04-07T15:22:33Z")

</div>

Thanks for the post..After extracting the values(var a),is there a way to assign var ‘a’ in JS to use it further?

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [April 7, 2016, 4:36pm UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/5 "2016-04-07T16:36:16Z")

</div>

Hi Vinay, I would be glad to help you. But please as a new, toplevel question? And be specific about what you are trying to accomplish. I don’t know what it means to say “is there way to assign var ‘a’?” a is already assigned. so you’ll have to elaborate.

but not HERE. I[n a new question please.](https://community.apigee.com/questions/ask.html)

---

<div class="post-metadata">

**Author:** ![API-Evangelist](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/api-evangelist/32/7651_2.png) [@API-Evangelist](https://discuss.google.dev/u/API-Evangelist)\
**Post date:** [April 7, 2016, 8:43pm UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/6 "2016-04-07T20:43:41Z")

</div>

Please check below post & help.

[https://community.apigee.com/questions/21512/extract-comma-separated-header-value.html](https://community.apigee.com/questions/21512/extract-comma-separated-header-value.html)

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [May 11, 2018, 7:13am UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/7 "2018-05-11T07:13:52Z")

</div>

Is there a simi writeup for Java?

---

<div class="post-metadata">

**Author:** ![dchiesa1](https://d3byx8b92xw3lq.cloudfront.net/user_avatar/discuss.google.dev/dchiesa1/32/15641_2.png) [@dchiesa1](https://discuss.google.dev/u/dchiesa1)\
**Post date:** [May 11, 2018, 4:25pm UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/8 "2018-05-11T16:25:53Z")

</div>

not that I know of. It should work in a more straightforward way in Java because a Java callout will receive a Collection when retrieving the context variable like “request.header.name-of-multi-valued-header”. And within Java you will be able to manipulate (read/write) that collection directly.

In JavaScript, you cannot access the native Java collection object, so there’s a need to transform it implicitly into a string, and perform string manipulation. In Java that’s not necessary.

If you have tried something and it’s not working, and you cannot figure out why, **please post a new question** (don’t reply to this with comment with a new question), and I’ll see if I can help you.

---

<div class="post-metadata">

**Author:** ![anonymous8](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@anonymous8](https://discuss.google.dev/u/anonymous8)\
**Post date:** [August 3, 2019, 8:47am UTC](https://discuss.google.dev/t/how-to-handle-multi-value-headers-in-javascript/17458/9 "2019-08-03T08:47:20Z")

</div>

hey [@Dino-at-Google](https://community.apigee.com/users/8595/dchiesa-1.html). Just noticed a couple things and wanted to confirm provide some updated info.

1. it appears that the following recommendation from above does NOT work for query params. Also, this property is not in the “flow variables” [docs](https://docs.apigee.com/api-platform/reference/variables-reference#request) for the headers or query params.

```auto
 var qp = context.getVariable('request.queryparam.myparam.values.string');

```

1. **this**  **doesn’t work** (as noted in your original post) and perhaps the documentation should be updated with the workaround (item 3 below) until it is fixed?

```auto
var qp = context.getVariable('request.queryparam.myparam.values')

```

I couldn’t get the above to coerce to a string until I saw your post about adding **+ ‘’**

1. **this does work**

```auto
var sort = context.getVariable('request.queryparam.myparam.values')+'';

```

Obviously these two are meant to work together for looping through the params. I was able to get it all working using the following:

```auto
var count = context.getVariable('request.queryparam.myparam.values.count');
var qp = context.getVariable('request.queryparam.myparam.values')+'';

//remove "[" and "]" from string and create the array
var params = qp.slice(1,-1).split(",");

//loop through the array
params.forEach(function(element, i) {
    //your code here
    //element has the value        
}

```

It would be great if we could have the following root issue fixed in a future release!

```auto
var qp = context.getVariable('request.queryparam.myparam.values')

```
