Search code examples
phpcodeigniterphp-5.4

PHP REQUEST_METHOD POST turns into GET


I'm trying to extend an old web application which uses CodeIgniter 1.7 (I know...) and am running into some trouble. I want a certain route to only be accessible via a POST request. So at the top of the function I have the following;

if ($_SERVER['REQUEST_METHOD'] != 'POST')
    die ('Wrong request method: ' . $_SERVER['REQUEST_METHOD']);

Locally, on PHP 7.0.14 with PHP's built-in web server, this works fine. In production, however, on a CPanel-managed server running PHP 5.4.25, it doesn't.

When sending a POST request to my route with Postman, I get the following;

Wrong request method: GET

PUT, PATCH, DELETE, ... requests all get recognised correctly. POST requests, however, seem to magically become GET requests.

POST data I sent with the request seems to disappear as well and is not to be found in either $_POST or $_GET.

It seems to not be related to the CodeIgniter framework however as when I call a file outside of the framework I get the same result.

Any thoughts?


Solution

  • I was making requests to http://domain whcih was set up to redirect to http://www.domain, which as it turns out turns POST requests into GET requests. And Postman does not notify the user of such redirects happening.

    If you're running into this problem, make sure to check whether there's any redirects happening.

    I wonder why this only happens with POST requests, however, and not with PUT, PATCH, DELETE, ... requests.

    Thanks to LSerni, Alex Blex and Vaviloff for pointing me in the right direction.