{"id":2279,"date":"2015-09-11T10:14:13","date_gmt":"2015-09-11T10:14:13","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2279"},"modified":"2015-09-11T10:14:13","modified_gmt":"2015-09-11T10:14:13","slug":"get-all-http-headers-in-javascript","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/09\/11\/get-all-http-headers-in-javascript\/","title":{"rendered":"Get all HTTP headers in Javascript"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushJScript.js\"><\/script>\n<p>The code below is a single function which can be plugged into any page to capture and store all the HTTP headers, available in the page request, as an associative array. The function also dumps the data to the page . No external library is required &#8211; this is pure javascript and thus runs in all browsers.<\/p>\n<p>A lot of people try to obtain the client IP address in javascript, but that is not possible. You need either an external service like MaxMind or a serverside script to get the client IP address.<\/p>\n<p>To display the data the function code assumes there is a DIV defined in the page with an id of &#8220;dump&#8221;.<\/p>\n<p><pre class=\"brush: js\">    function getHeaders() {\r\n\tvar req = new XMLHttpRequest();\r\n\treq.open(&#039;GET&#039;, document.location, false);\r\n\treq.send(null);\r\n\t\r\n\t\/\/ associate array to store all values\r\n\tvar data = new Object();\r\n\t\r\n\t\/\/ get all headers in one call and parse each item\r\n\tvar headers = req.getAllResponseHeaders().toLowerCase();\r\n\tvar aHeaders = headers.split(&#039;\\n&#039;);\r\n\tvar i =0;\r\n\tfor (i= 0; i &lt; aHeaders.length; i++) {\r\n\t    var thisItem = aHeaders[i];\r\n\t    var key = thisItem.substring(0, thisItem.indexOf(&#039;:&#039;));\r\n\t    var value = thisItem.substring(thisItem.indexOf(&#039;:&#039;)+1);\r\n\t    data[key] = value;\r\n\t}\t    \r\n\r\n\t\/\/ get referer\r\n\tvar referer = document.referrer;\r\n\tdata[&quot;Referer&quot;] = referer;\r\n\r\n\t\/\/get useragent\r\n\tvar useragent = navigator.userAgent;\r\n\tdata[&quot;UserAgent&quot;] = useragent;\r\n\r\n\t\r\n\t\/\/extra code to display the values in html\r\n\tvar display = &quot;&quot;;\r\n\tfor(var key in data) {\r\n\t    if (key != &quot;&quot;)\r\n\t\tdisplay += &quot;&lt;b&gt;&quot; + key + &quot;&lt;\/b&gt; : &quot; + data[key] + &quot;&lt;br&gt;&quot;;\r\n\t}\r\n\tdocument.getElementById(&quot;dump&quot;).innerHTML =  display;  \r\n    }\t\r\n<\/pre><\/p>\n<p>You can see the<a href=\"http:\/\/truelogic.org\/headers.html\" target=\"_blank\"> code in action here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The code below is a single function which can be plugged into any page to capture and store all the HTTP headers, available in the <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/09\/11\/get-all-http-headers-in-javascript\/\" title=\"Get all HTTP headers in Javascript\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2282,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,16],"tags":[],"class_list":["post-2279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-www-stuff"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2279"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2279\/revisions"}],"predecessor-version":[{"id":2283,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2279\/revisions\/2283"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2282"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}