Search code examples
phprestcodeigniterpostpostman

$_POST variable from Code Igniter is empty when calling the endpoint with Postman


I have a web app built with Code igniter, that is working, and want to be able to call some functions from a certain controller via postman.
Because I am using postman (for test purposes), I've ignored the whole login/autentication logic and only focusing on reaching my controller's function.
The problem is that my $_POST (and my $this->input->post()) variable is empty when arriving at the controller function. I've tried logging this variable in the Config.php file (that I believe is the first file to be called when using Code Ignitier) and it's already empty at that point.

How am I testing?
On my public function, I am printing the result from $this->input->post(). The result is empty when calling the function from postman but when I am calling from within my web app the result shows the payload passed through the request.
From that moment, I went up from the callstack until I reached the Config.php file, that's where I tried to print the value from $_POST.
I've also tried using json_decode(file_get_contents('php://input'), true); but without any success.

My php code:

public function myFunction()
    {
        
        $x = json_decode(file_get_contents('php://input'), true);
        var_dump($x);
        var_dump($_POST);
        var_dump($this->input->post('payload'));
}

In summary:

  • Tried diferente type of Content-type with postman (as showm below)
  • also tried diferent way to display the post body (as shown above)
  • Tried to log the post body in diferent part of the code execution

The request:
POST: http://localhost/myController/myFunction
Headers:
username: my_user
enter image description here

Result from calling myFunction:

NULL
array(0) {}
NULL

Why is $this->input->post() and $_POST empty on that both cases when using postman?

Edit: As sugested, I've tried using form-data and x-www-form-urlencoded but the result is still the same.
enter image description here enter image description here


Solution

  • My case was an issue with the server (nginx) configuration. When any call hit the endpoint, the server redirected with a 301 status code (I found out about this status code when I used ngrok to provide a public call for the api).
    In Postman, if you use the option Follow original HTTP Method, it'll successfully make the request. That's when I noticed that something related to redirected was happening.
    The problem is that 301 redirect doesn't maintain the post body from the request.
    The solution was to update the configuration from the server to redirect with a status code of 307, which will maintain the post body.