i have a question about how we can capture request headers(only request not response) in a browser using javascript(specifically Authentication header related to http basic authentication ). have you any idea about these?? thanks all
Asked
Active
Viewed 3,532 times
2
-
Here your answer http://stackoverflow.com/questions/220231/accessing-http-headers-in-javascript – Ivan Dyachenko Sep 05 '12 at 13:20
-
please notice that HTTP REQUEST headers no HTTP RESPONSE headers! – user993975 Sep 05 '12 at 13:31
2 Answers
0
If you're talking about seeing the request headers sent to you, it can't be done. Accessing the web page's HTTP Headers in JavaScript

Community
- 1
- 1

Andrew Latham
- 5,982
- 14
- 47
- 87
0
As Javascript is executed in an HTTP response, you will only get access to the headers which are exposed via the in-built browser/document classes, e.g. document.referrer
, navigator.language
and similar.
Others you'll have to pass through manually, e.g. by setting a cookie or hidden field, or coding up an XHR.

Dave R.
- 7,206
- 3
- 30
- 52
-
please explain more. i want capture my http request header.how browser set authentication http header tag ( only for the first time )every time i see a basic authentication page protected page.from where browser remembter my credential? and how can get this by javascript? – user993975 Sep 05 '12 at 13:29
-
Here's some further information on a purely Javascript method of HTTP basic auth: http://stackoverflow.com/questions/491914/pure-javascript-code-for-http-basic-authentication However, I'd recommend using cookies instead. Here's a topic that discusses this: http://stackoverflow.com/questions/5052607/cookies-vs-basic-auth – Dave R. Sep 05 '12 at 13:36
-
(You edited your previous comment to add further information.) You cannot get the request's Authorization header without passing it through by other means, for example by saving it to a hidden form field. However, you should not need to do this manually: once authentication has been passed, the client (browser) will use this information for subsequent requests automatically. If you want to do the initial login from Javascript, you'll have to encode the header yourself - the previous link describes the process. – Dave R. Sep 05 '12 at 13:51