The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Post parameters

Giulio's Avatar

Giulio

03 Mar, 2016 08:48 PM

Hello

I have my api which validates required parameters:
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();
    }
}

It works fine if I use standard web app or postman for example.
The request sends the parameters on [slim.request.form_hash] as this:
[slim.request.form_hash] => Array
         (
              [IMEI] => aa
          )

What I'm facing now is quite weird.

I'm developing an embedded application, using a GSM Module and I'm building the post request manually.
My sending information is:

POST /devices/deviceRegister HTTP/1.1
Host: apidvc.xxxxxxxx.cc
Accept: /
Connection: Keep-Alive
Content-Type: application/json
User-Agent: SIMCOM_MODULE
Content-Length: 14

{"IMEI":"aa"}

Where IMEI is my body parameter.

My backend app receives the parameter on [slim.input] as this:
                    [slim.input] => Array
                        (
                            [IMEI] => aa
                        )

                    [slim.errors] => Resource id #5
                    [slim.input_original] => {"IMEI":"aa"}
                    [slim.request.form_hash] => Array
                        (
                        )

And my validation function returns parameter missing.

Why the parameter sits in different places ?
How can I solve it ?

Thank you

  1. 1 Posted by Giulio on 03 Mar, 2016 08:53 PM

    Giulio's Avatar

    More info:

    Call from postman printing $_REQUEST:
    array(1) {
      ["IMEI"]=>
      string(2) "aa"
    }
    {"error":false,"DEVICE_ID":1,"DEVICE_API_KEY":"bf8855e291911edb41705a848a33ba41"}

    Call from my embedded application:
    array(0) {
              }
               {"error":true,"message":"Required field(s) IMEI is missing or empty"}

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac