Deprecated `Headers.getAll()` API reimplementation

185 Views Asked by At

The Headers.getAll() method seems to have provided a reliable way of parsing multivalued http headers. However, according to documentation the method has been deprecated and removed from specification. There seems to be no method left that would support parsing multivalued headers. The Headers.get() method seems to return an unparsed string containing several values. How do I reimplement the deprecated method in a reliable manner?

1

There are 1 best solutions below

3
cyberixae On

By looking at various sources I get the idea that comma is used as a separator and the values themselves can never contain commas within them. It also seems that whitespace before each individual value should be ignored but whitespace at the end of each value should perhaps preserved. Based on this I'm inclined to implement it as follows.

Headers.prototype.getAll = function(name) {
  return this.get(name).split(',').map(v => v.trimStart());
}