How to get the Request Headers data of the current page using JavaScript

requirements:

want to add a js script to the page to get the Headers information of the current page.

problems encountered:

now you can only get Response Headers data, using the following code:

var req = new XMLHttpRequest();
req.open("GET", document.location.href, false);
req.send(null);
var headers = req.getAllResponseHeaders();
console.log(headers);

but the RequestHeaders data cannot be obtained, and no getAllRequestHeaders ()-like method is found.

I wonder if the js script can get the information about request header on the page.
Thank you, everyone!

Mar.06,2021

there is a method: getAllResponseHeaders ()

reference: https://stackoverflow.com/que.


  1. Request Headers is not the Headers you sent, so his information really depends on how you set it, so why you need to get his information.
  2. XMLHttpRequest is just a Web API . This is not to say that this code sends a http request. It is the code that asks the browser to send this request. Things such as adding cookie adding user-agent can't be sent to you before sending (there is no need), and it doesn't make sense to give it to you after sending it (you can't change it), so there is no such interface now. It's unlikely to happen in the future.
Menu