본문 바로가기

Language/JavaScript

header 정보 json 형태로 가져오기

function parseHttpHeaders(httpHeaders) {

return httpHeaders

.split("\n")

.map(x => x.split(/: */, 2))

.filter(x => x[0])

.reduce((ac, x) => {

ac[x[0]] = x[1];

return ac;

}, {});

}

 

var req = new XMLHttpRequest();

req.open("GET", document.location, false);

req.send(null);

var headers = parseHttpHeaders(req.getAllResponseHeaders());

console.log(headers);