0

It's easy enough to use AJAX from a browser to an external address, (i.e. external to the browser, even if it's localhost) but I have a different question.

Is there some kind of object or service that would allow a browser to make (or mock) an HTTP request to itself, e.g. from JavaScript?

E.g. from Firefox I can see the following raw request:

GET /headers.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0

And by using a PHP script (the aforementioned headers.php in the GET request):

<pre>
<?php
print_r(apache_request_headers ());

I can see the following on the page:

[Content-Type] => 
[Content-Length] => 0
[Upgrade-Insecure-Requests] => 1
[User-Agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:49.0) Gecko/20100101 Firefox/49.0
[Host] => localhost
[Accept-Language] => en-GB,en;q=0.5
[Accept-Encoding] => gzip, deflate
[Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
[Connection] => keep-alive
[Cache-Control] => max-age=0

Can I get the same information via JavaScript, without making a call to a server script?

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
  • 2
    What do you mean by *"to itself"* What is the use-case you've got in mind ? Like that I can't really see what the browser does serve that you could request for... UserAgent stylesheets maybe ? – Kaiido Aug 10 '17 at 05:48
  • @Kaiido Use case: make a fake request, get the `Accept-Languages` request header which is otherwise unavailable without a server to echo the value back to the browser. – CJ Dennis Aug 10 '17 at 05:51
  • @CJDennis Are you trying to make a `HEAD` or `OPTIONS` request [Why does Fetch API Send the first PUT request as OPTIONS](https://stackoverflow.com/q/42311018/), https://stackoverflow.com/questions/42311018/why-does-fetch-api-send-the-first-put-request-as-options/42311206#comment71821815_42311018? – guest271314 Aug 10 '17 at 05:53
  • 1
    Not gathering what you are trying to achieve? What is requirement? – guest271314 Aug 10 '17 at 05:59
  • Isn't this `Accept-Languages` header the same as `navigator.languages` ? – Kaiido Aug 10 '17 at 06:02
  • @Kaiido No, that's the point. – CJ Dennis Aug 10 '17 at 06:02
  • 1
    Ok so now can you tell us how you get this header when it comes from a real server ? Does a dummy request to `about:blank` or even `data:text/plain,` works ? Also, your question would benefit from an [edit] stating exactly what you are trying to achieve and why (e.g "I want to get the Accept Languages request header without the use of a server") instead of the current non-sense. – Kaiido Aug 10 '17 at 06:06
  • You can the request including `Accept-Language` header as a `.har` file at browser. Though the original Question does not ask how to get `Accept-Language` header, see https://stackoverflow.com/help/how-to-ask. It is currently unclear what you are asking. – guest271314 Aug 10 '17 at 06:13
  • @guest271314 How do you access the `.har` file from JavaScript? – CJ Dennis Aug 10 '17 at 06:15
  • You can create an extension or use `Network` tab at `DevTools`. Though that is beyond the scope of the actual Question. – guest271314 Aug 10 '17 at 06:16
  • So according to your edit, you have no way to get this info from javascript even when the request is made to a real sever (it's either dev-tools or server-side). – Kaiido Aug 10 '17 at 06:58
  • @Kaiido I know of no way. I'm not saying there is no way, just that if there is, I don't know it. If you know of a way would you be so kind as to tell me? I'm not talking about parsing the response from the server. – CJ Dennis Aug 10 '17 at 07:01
  • No and that's my point : you are trying to make something that you already can't do, in a different way. Your question should really be *"Is there a way to get the request headers from javascript"* And this question has probably already been asked before, but I'm afraid the answer will be "no" because XHR doesn't expose it. – Kaiido Aug 10 '17 at 07:05
  • https://stackoverflow.com/questions/220149/how-do-i-access-the-http-request-header-fields-via-javascript – Kaiido Aug 10 '17 at 07:10

1 Answers1

0

You can request either a Blob URL or a data URI see Does Stack Overflow have an "echo page" to test AJAX requests, inside a code snippet?, or use ServiceWorker to serve a specific Response Chrome extension: Block page items before access. An empty string passed to either XMLHttpRequest.open() or fetch() requests the current URL.

guest271314
  • 1
  • 15
  • 104
  • 177