How To Make A Json Object Out Of Getallresponseheaders Method
I am currently writing a google chrome extension, and I need to find out information about websites' response headers. In order to do this, I used the getAllResponseHeaders method,
Solution 1:
See https://msdn.microsoft.com/en-us/library/ms536428(v=vs.85).aspx. The return headers are a crlf delimited string where each line contains key values separated by a colon. You will probably have to adjust the code below to account for whitespace.
var arr = allResponseHeaders.split('\r\n');
var headers = arr.reduce(function (acc, current, i){
var parts = current.split(': ');
acc[parts[0]] = parts[1];
return acc;
}, {});
Solution 2:
I use the following function to extract all response headers as JS object using getAllResponseHeaders()
:
functiongetResponseHeaderMap(xhr) {
const headers = {};
xhr.getAllResponseHeaders()
.trim()
.split(/[\r\n]+/)
.map(value => value.split(/: /))
.forEach(keyValue => {
headers[keyValue[0].trim()] = keyValue[1].trim();
});
return headers;
}
Post a Comment for "How To Make A Json Object Out Of Getallresponseheaders Method"