0

I want to track request and modify it using javaScript or jquery. how can i add header in every request sent by browser to server.

Very first time when i hit a url from browser, Request is sent to server and a HTML page is returned as a response. In this html page there are so many css, img and other links are given to access resource. When browser sends request to access resource. All the request are displayed in network section of developer tool of browser.

I need to send header with every request from browser using js.

Rahul sharma
  • 399
  • 1
  • 5
  • 14
  • 1
    I'd suspect you'd need some sort of plugin (maybe Greasmonkey) to do this. You can't just hijack everything on the page (e.g. images, scripts) as you can't guarantee the load order. Even if you could, there's not a simple way of 'appending' headers to all image requests. You'd need to at least use Angular here http://stackoverflow.com/questions/21841426/add-http-header-in-javascript-to-requests-for-images – G0dsquad Mar 07 '17 at 16:32
  • +1 because it's a valid question. Suppose you wanted to intercept every http call made by the browser? I don't think there is any easy way...but it's an interesting question ( years later, now, but still interesting ) – Nick Perkins Dec 02 '20 at 15:28

1 Answers1

0
var request = new XMLHttpRequest();
request.open("GET" /*(for example)*/, url);
request.setRequestHeader("header", "value");
request.send();

This is a possibility to add an header to a HTTP-Request in js. Does this answer your question?

  • Thanks for quick reply!! But it does not answer my question. I want to track and modify every request sent by browser.. if browser trying to access img file or css or js file from server – Rahul sharma Mar 07 '17 at 16:26
  • Plz Read my question again. – Rahul sharma Mar 07 '17 at 16:29
  • Ok, now I understand your question, but I don#t know how you could implement this in javascript. There are several add-ons like https://addons.mozilla.org/en-US/firefox/addon/modify-headers/, which allow you to change the headers manually. – newb.61 Mar 07 '17 at 16:35