Skip to content

Instantly share code, notes, and snippets.

@jherax
Last active April 19, 2021 15:34
Show Gist options
  • Save jherax/5f919aad8ea7535790f3d311c26a6342 to your computer and use it in GitHub Desktop.
Save jherax/5f919aad8ea7535790f3d311c26a6342 to your computer and use it in GitHub Desktop.
Gets all Response Headers for the current document
/**
* Gets all Reponse Headers for the current document.
* @returns An object cointaining the key-value pairs for each header
*/
function getHeaders(url = document.location.href) {
const req = new XMLHttpRequest();
req.open("GET", url, false);
req.send(null);
let key: string;
let value: string;
let colon: number;
const headers = req.getAllResponseHeaders().split(/\n/).filter(Boolean);
const data = headers.reduce((pairs, header) => {
// some values contains ":" then, the split method is not recommended
colon = header.indexOf(":");
key = header.substring(0, colon);
value = header.substring(colon + 1);
pairs[key] = value;
return pairs;
}, new Object());
data["Referer"] = document.referrer;
data["UserAgent"] = navigator.userAgent;
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment