XMLHttpRequest.getAllResponseHeaders()

The XMLHttpRequest.getAllResponseHeaders() method returns all the response headers, separated by CRLF, as a string, or null if no response has been received. If a network error happened, an empty string is returned.

Note: For multipart requests, this returns the headers from the current part of the request, not from the original channel.

Syntax

var headers = XMLHttpRequest.getAllResponseHeaders();

Parameters

None.

Return value

A ByteString representing all of the response's headers (except those whose field name is Set-Cookie or Set-Cookie2) separated by CRLF, or null if no response has been received. If a network error happened, an empty string is returned.

Example

var request = new XMLHttpRequest();
request.open("GET", "foo.txt", true);
request.send();
request.onreadystatechange = function() {
  if(this.readyState == this.HEADERS_RECEIVED) {

    //
    // RAW STRING OF HEADERS
    //
    var headers = request.getAllResponseHeaders(); 
    console.log(headers);
    // "date: Fri, 08 Dec 2017 21:04:30 GMT\r\ncontent-encoding: gzip\r\nx-content-type-options: nosniff\r\nserver: meinheld/0.6.1\r\nx-frame-options: DENY\r\ncontent-type: text/html; charset=utf-8\r\nconnection: keep-alive\r\nstrict-transport-security: max-age=63072000\r\nvary: Cookie, Accept-Encoding\r\ncontent-length: 6503\r\nx-xss-protection: 1; mode=block\r\n"


    //
    // ARRAY OF HEADERS
    //
    var arr = headers.trim().split(/[\r\n]+/);


    //
    // MAP OF HEADERS
    //
    var map = {};
    arr.forEach(function (line) {
      var parts = line.split(': ');
      var header = parts.shift();
      var value = parts.join(': ');
      map[header] = value;
    });

  }
}

Specifications

Specification Status Comment
XMLHttpRequest
The definition of 'getAllResponseHeaders()' in that specification.
Living Standard WHATWG living standard

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 (Yes) 1.0 (1.7 or earlier)[1] 5[2]
7
(Yes) 1.2
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? 1.0 (Yes) 1.0 (1.0) ? ? ?

[1] Starting from Firefox 49, empty headers are returned as empty strings in case the preference network.http.keep_empty_response_headers_as_empty_string is set to true, defaulting to false. Before Firefox 49 empty headers had been ignored. Since Firefox 50 the preference defaults to true.

[2] This feature was implemented via ActiveXObject(). Internet Explorer implements the standard XMLHttpRequest since version 7.

See also

Document Tags and Contributors

 Contributors to this page: coolaj86, Makyen, erikadoyle, teoli, GabRoyer, Sebastianz, MashKao, jsx
 Last updated by: coolaj86,