-1

Let's say I have an test.html file that I can either call by a POST request or a GET request. In this file I want to have a script that tells the type of the request. How should I do?

Thank you for your help.

JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61
  • Possible duplicate of [Client-side detection of HTTP request method](https://stackoverflow.com/questions/121218/client-side-detection-of-http-request-method) – Luca Kiebel Jul 22 '18 at 15:10
  • You could inject that information from the server side before sending the response. – Pubudu Dodangoda Jul 22 '18 at 15:12
  • Yes, but I forgot to mention that I wanted to know if it was possible without any webserver. – JacopoStanchi Jul 22 '18 at 15:13
  • @JacopoStanchi — You can't make POST or GET requests without a webserver. – Quentin Jul 22 '18 at 15:14
  • I don't agree, the browser is doing a GET request to `test.html` when you open it in the browser, it doesn't require any webserver. And if I submit a form with the POST method to `test.html` it doesn't require a webserver either. – JacopoStanchi Jul 22 '18 at 15:18
  • @JacopoStanchi — In each of those cases, the browser is just opening a local file without using any HTTP method. – Quentin Jul 22 '18 at 15:21
  • No, I mean I host the file on, let's say, BitBalloon. It doesn't have a webserver but I can send requests. – JacopoStanchi Jul 22 '18 at 15:22
  • @JacopoStanchi — BitBalloon has a web server. (Unless you run the files locally without using a webserver, in which case it accesses the local file system without making HTTP requests, so won't use GET or POST.) – Quentin Jul 23 '18 at 13:42
  • I'm still not convinced you need to host an html file on a server to open it with the HTTP protocol, but let's drop the BitBalloon example. If I open locally `file1.html` containing a POST form redirecting to `file2.html`. When I submit the form, it redirects well to file2. What you are telling me is that the request sent is not an HTTP request by a pseudo-protocol file request? I.e the data of the form is not sent, there is just a redirect. – JacopoStanchi Jul 23 '18 at 14:14
  • @JacopoStanchi — There's nowhere to send the data to. The browser just loads the file from the local filesystem. – Quentin Jul 23 '18 at 14:46
  • I understand better then. It's the browser that sends the request, but he can only send it to a webserver and not an HTML file. – JacopoStanchi Jul 23 '18 at 14:48

1 Answers1

3

There is no way for client-side JS to determine the request method used to load the current page.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • But it could be possible if W3C wanted to add this in Javascript, right? In Firefox developer, which is client-side, we can see all of the requests sent. – JacopoStanchi Jul 22 '18 at 15:10
  • 2
    the navigator and client side js are not the same thing – Luca Kiebel Jul 22 '18 at 15:10
  • 1
    @JacopoStanchi — If browser vendors provided an API that gave you the information, then yes, it would be possible, but they do not, so it isn't. The developer tools in browsers have access to a lot more information than JS provided by the authors of webpages the user visits. – Quentin Jul 22 '18 at 15:11
  • That would be very convenient because in many cases, you don't necessarily want a database for your web app and you could theorically do all of the operations if only you had access to the request type and body. – JacopoStanchi Jul 22 '18 at 15:15