contool
contool

Reputation: 1074

SLIM PHP api 400 error - $_REQUEST not working

I'm beginning to learn how to properly use REST API's following a popular tutorial here

When trying to create a simple POST api I keep running into a 400 error and I haven't found any answers online as of yet.

API is as follows:

$app->post('/test', function() use ($app) {
//             check for required params
            verifyRequiredParams(array('email'));
            echo "Success";
});

Which attempts to verify the 'email' parameter with this block of code, unchanged from the tutorial:

function verifyRequiredParams($required_fields) {
    $error = false;
    $error_fields = "";
    $request_params = array();
    $request_params = $_REQUEST;
    // Handling PUT request params
    if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
        $app = \Slim\Slim::getInstance();
        parse_str($app->request()->getBody(), $request_params);
    }
    foreach ($required_fields as $field) {
        if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
            $error = true;
            $error_fields .= $field . ', ';
        }
    }

    if ($error) {
        // Required field(s) are missing or empty
        // echo error json and stop the app
        $response = array();
        $app = \Slim\Slim::getInstance();
        $response["error"] = true;
        $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
        echoRespnse(400, $response);
        $app->stop();
    }
}

The code always fails with a 400 error code because it seems that the $_REQUEST function is returning an empty array. Any idea's what could be going wrong? I'm developing on an ubuntu server running apache.

The Slim API framework is set up correctly as I can call a very simple call through Google's Advanced Rest Client which echos a response correctly. And I am definitely passing the correct parameters into the api.

UPDATE: I've also tried running the $_REQUEST and $_GET functions directly from the api and it just returns an empty string like this:

Array
(
)

I wonder if there's some sort of apache config file that's blocking data to these functions or something?

Upvotes: 2

Views: 1762

Answers (2)

mluckham
mluckham

Reputation: 1

For the tutorial to receive $name = $app->request->post('name');

the POST parameters have to be in Payload data name=yourname&password=pass&[email protected] application/x-www-form-urlencoded

If you provide the parameters after the URL but not in the payload data, that is for a GET request. In such a case the tutorial partly works (it sees the parameters to verify required parameters are present) but it is looking for the parameters "anywhere" when it should only be looking in the payload parameters for POST.

Upvotes: 0

Sean Dev
Sean Dev

Reputation: 1309

Possible Problem: Have you double checked that your selected Content-Type in the request headers is right? I suspect the client is perceiving a malformed markup language being sent from the server side.

Possible Solution: Change Content-Type: application/x-www-form-urlencoded to application/JSON or vice versa.

Upvotes: 2

Related Questions