⬆️ ⬇️

Why POST turns into GET

For a long time I tried to understand why the following code, when a button is pressed, gives a request using the GET method, although POST is clearly indicated.



< html >

< head >

</ head >

< body >

< form method ="POST" action ="http://myhost/mydir" >

< input type ="submit" value ="→" />

</ form >

<? echo $_SERVER['REQUEST_METHOD'] ? >

</ body >

</ html >




* This source code was highlighted with Source Code Highlighter .




It turns out that Apache, with a POST request to the URL of the folder without a final slash, redirects to the URL with a slash and at the same time the request is transformed into GET. If you put a slash, the effect disappears.

')

Conclusion: In the action form, always put a slash at the end of the folder URL, and better use the file URL.



UPD : As Nikita suggests, this feature (redirection to the URL with a slash at the end) is controlled in apache 2.2 by the DirectorySlash directive, it is turned on by default and is not recommended to be disabled.



UPD2 : It turns out this scheme

1. We send POST to mysite / mydir

2. The server answers us 301 to mysite / mydir / (why this is correct, described in the Apache documentation )

3. The browser makes GET without parameters on mysite / mydir / (this is how it should be done, it is described in RFC 2616)



As a result, instead of our POST with parameters, we get GET without parameters. Everything is logical, but somewhat unexpected.

Source: https://habr.com/ru/post/55403/



All Articles