0

Is it possible to read all the HTTP Request/Responses' header and body in a webpage through javascript or through any of its framework?

Screenshot of the required field (opens in the same window)

For example:- Like the way I could view/copy them, through my browser's developer tools.

To my understanding, if I could get hold onto an object or event that fires these requests then I can access the responseText property to fulfill my requirement.

My question is how do I do that? Is it even possible to get all the responseText for all the responses received in my webpage?

(As it has been rendered successfully, then possibly I should be able to access them as well, isn't it?)

I'm just a beginner, so not sure if my question is meaningful. Thanks for all the replies.

Prakash M.
  • 479
  • 8
  • 26
  • Possible duplicate of [Accessing the web page's HTTP Headers in JavaScript](http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript) – jdphenix Nov 24 '16 at 18:46
  • Yes @jdphenix, I tried that solution and I did not get the expected result, so I raised this question to clarify. I got this result though. 'content-type: text/html; charset=utf-8 date: fri, 25 nov 2016 04:26:05 gmt connection: keep-alive content-length: 3102' How can I get more details from the headers? Can anyone help me out? – Prakash M. Nov 25 '16 at 04:28
  • Also is it possible to read the response body of a particular request's response through the script? How to get an object of a particular request/response, in order to access responseText? – Prakash M. Nov 25 '16 at 04:33

2 Answers2

1

If I'm understanding correctly, you're asking how to retrieve detailed network traffic information for a given website. This is browser specific: Chrome for example exposes the chrome.devtools.network object which you can interrogate. See https://developer.chrome.com/extensions/devtools_network

  • Thanks for your effort. I have updated the question and the comments in other replies. Your answer is correct as well. I'm looking for a more generic solution. – Prakash M. Nov 25 '16 at 04:50
0

Try this following javascript code:

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

it will get all the HTTP headers.

knjght0011
  • 35
  • 1
  • 8
  • Thanks for the effort @knjght0011. I tried that solution and I did not get the expected result, so I raised this question to clarify. I got this result though - 'content-type: text/html; charset=utf-8 date: fri, 25 nov 2016 04:26:05 gmt connection: keep-alive content-length: 3102' How can I get more details from the headers? Can anyone help me out? – Prakash M. Nov 25 '16 at 04:29
  • Also is it possible to read the response body of a particular request's response through the script? How to get an object of a particular request/response, in order to access responseText? – Prakash M. Nov 25 '16 at 04:33