1

I need to get the text of a custom HTTP Header with Javascript, the header I need is not the User Agent, I already have that covered with this code:

var userAgent = navigator.userAgent

What I need is to get the content (like: Custom Header Text) of a custom header called (for example): CustomHeader.

The Header is sent by the browser when accessing a website (and showing it to the user) with the Javascript code on the website, so I guess I would have no problem with the same-origin policy, since it's only a website with a Javascript code on it (with script tags).

Is it possible? If so, how can I get the content of that header with Javascript?

Thanks a lot.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Minion
  • 2,497
  • 4
  • 27
  • 29
  • You're trying to get a header that's sent by the browser, or a header that's returned by the server? – Barmar Apr 29 '16 at 18:11
  • The header is sent by the browser – Minion Apr 29 '16 at 18:12
  • Where is the custom header coming from? Why can't you get it from that code? – Barmar Apr 29 '16 at 18:12
  • An Android app, is sent like this: extraHeaders.put("CustomHeader", "Custom Header Text"); – Minion Apr 29 '16 at 18:13
  • Unless the server sends the information back to the client, I don't think there's any way to get it on the client. – Barmar Apr 29 '16 at 18:14
  • I checked with test header and I'm using Chrome inspection tools and the app is sending the header and it appears as customheader: Custom Header Text, it seems like it gets all in lowercase, since Javascript can get the user-agent header, I thought that it can also get a custom one sent by the browser as well (and at the same time) – Minion Apr 29 '16 at 18:20
  • 1
    It's not getting the User-Agent header from the current page, `navigator.userAgent` is a property of the browser that tells it what it should send when making future requests. – Barmar Apr 29 '16 at 18:26
  • That's bad... thanks anyway, can you write that as an answer to accept it? Thanks. – Minion Apr 29 '16 at 19:18

1 Answers1

-1
var request = new XMLHttpRequest();
req.open('GET', document.location, false);
req.send(null);
var headers = req.getAllResponseHeaders();

You can read more here: Accessing the web page's HTTP Headers in JavaScript

Community
  • 1
  • 1
Slack
  • 9
  • 1
  • 1
    He's clarified that he's asking about headers that were sent by the client, not headers that come back from the server. – Barmar Apr 29 '16 at 18:14