javascriptphphtmlangularjsflightphp

The $_POST data is empty even though I'm getting a 200 response on the POST?


I've got a PHP application that uses AngularJS for the client, FlightPHP for REST services, and PHP in the back end.

However, even though I'm issuing a POST with this code here:

$scope.registerUser = function() {
    $http({
        method: 'POST',
        url: '/user/register',
        data: $scope.user
    }).success(function(data) {
                if (data.result === undefined) {
                    setAjaxMessage(data, false);
                    return;
                }

                if (data.result === 0) {
                    setAjaxMessage(data.message, true);
                }
                else {
                    setAjaxMessage(data.message, false);
                }
            }).error(function(data) {
                setAjaxMessage(data, false);
            });
};

and I'm getting a successful POST message via Firefox with the Params -> Request payload tab set to this data:

{"displayname":"user1","email":"user1@gmail.com","password":"abc123"}

when I issue this server-side:

Flight::route('POST /user/register', function() {
    echo var_dump($_POST);
    return;

    // register the user
    require 'core/register.php';
});

I get this:

array (size=0)
  empty

What gives here? I've never had an issue with the $_POST data before. I have a feeling it has something to do with FlightPHP, but I can't put my finger on it. It's getting into the handler for the POST as you can see.


Solution

  • Alright, this one is a bit funky. But it is because of FlightPHP. Normally, the answer provided by Mike Brant would be 100% correct! However, FlightPHP reads that input before I can, and according to the PHP documentation it can only be read once.

    So, where does FlightPHP put it? Ha, now that's an odd one. They put it in a property named body, so I had to do this to grab those JSON values:

    $vars = json_decode(Flight::request()->body, true);
    

    The Flight::request()->body returns the JSON string. But then that needs decoded and turned into an associative array, so that's why I'm passing the true as the second parameter.