I found no evidence that there is a client-side API to get to know the headers the browser is sending for page requests.
Here is information I found about what headers are being sent:
https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending/
Other approach is via a server echo tool
To see the headers your browser is sending to the server when it retrieves a page, you can use JavaScript in combination with a server-side script or a browser's developer tools. Here’s a method using JavaScript with Fetch API and a server-side echo service:
Using JavaScript Fetch with an Echo Service
You can make a fetch
request to an echo service that simply returns the headers you sent to it. There are public APIs designed to return the headers sent in a request, such as https://httpbin.org/headers
.
Here is how you can do it:
javascript
Copy code
fetch('https://httpbin.org/headers')
.then(response => response.json())
.then(data => console.log(data.headers))
.catch(error => console.error('Error:', error));
Explanation:
- Fetch API: This is a modern JavaScript API to make network requests similar to
XMLHttpRequest
. - httpbin.org/headers: This service echoes back the headers it received. You can replace this with any server that returns request headers.
- console.log(data.headers): This prints the headers to the console.