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, but I need to put it in a JSON object. Unfortunately, I keep getting the error message SyntaxError: Unexpected token D in JSON at position 0 at main
.
Here is the code I am using to do this so far:
xmlhttp.open("GET", url, false);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
allResponseHeaders = xmlhttp.getAllResponseHeaders();
}
};
xmlhttp.send();
var responseHeaders = JSON.parse(allResponseHeaders);
obj.headers = responseHeaders;
When I put an alert immediately after the allResponseHeaders = xmlhttp.getAllResponseHeaders();
call, the alert shows that the call was a success; the response headers have all been retrieved. The first response header is the Date, which I think has to do with the Unexpected token D
part of my error message, but I don't know why it won't parse properly. How do I fix this? Thanks in advance.
EDIT: I would like my JSON object to look something like this:
{
"headers": {
"Date": "June 20, 2016",
"Content-Type": "charset=UTF/8",
...
}
}
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;
}, {});