const request = new XMLHttpRequest();
request.open("GET", "foo.txt", true);
request.send();
request.onreadystatechange = () => {
if (request.readyState === request.HEADERS_RECEIVED) {
// Get the raw header string
const headers = request.getAllResponseHeaders();
// Convert the header string into an array
// of individual headers
const arr = headers.trim().split(/[\r\n]+/);
// Create a map of header names to values
const headerMap = {};
arr.forEach((line) => {
const parts = line.split(": ");
const header = parts.shift();
const value = parts.join(": ");
headerMap[header] = value;
});
}
};XMLHttpRequest: getAllResponseHeaders() method
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
const contentType = headerMap["content-type"];