When I make a request to http://www.example.com
, why does I see http://www.example.com/
in the webRequest.onBeforeRequestListener?
For example:
chrome.webRequest.onBeforeRequest.addListener(
details => console.log('Sending request to', details.url),
{ urls: ['<all_urls>'] });
fetch('http://www.example.com');
will print
Sending request to http://www.example.com/
That is consistent with the request URL shown in the network request monitor. For example, if I take it and convert it to a curl command, the request looks like this:
curl 'http://www.example.com/' -H 'Accept: */*' -H 'Connection: keep-alive'
-H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9'
-H 'User-Agent: ...' --compressed
So, the original request that goes out is for http://www.example.com/
not for http://www.example.com
. That decision must have been made in the browser, not by the server.
The same behavior also occurs when using XMLHttpRequest
instead of fetch
. In my example, I used Chrome, but on Firefox it is the same.
Questions:
- Why does the browser change it automatically? It also happens with other URLs. From my understanding, adding a trailing slash will often work, but in general, it is a breaking change.
- If I want to filter in the
onBeforeRequest
listener for the current request to a specific URL, how can you reliably match it? For instance, just checking whether the URLs are identical will fail. - Are there more rewrite URL rules in the browser to be aware of?