user4407686
user4407686

Reputation:

How to access a JSON request body of a POST request in Slim?

I'm just a newbie in the Slim framework. I've written one API using Slim framework.

A POST request is coming to this API from an iPhone app. This POST request is in JSON format.

But I'm not able to access the POST parameters that are sent in a request from iPhone. When I tried to print the POST parameters' values I got "null" for every parameter.

$allPostVars = $application->request->post(); //Always I get null

Then I tried to get the body of a coming request, convert the body into JSON format and sent it back as a response to the iPhone. Then I got the parameters' values but they are in very weird format as follows:

"{\"password\":\"admin123\",\"login\":\"[email protected]\",\"device_type\":\"iphone\",\"device_token\":\"785903860i5y1243i5\"}"

So one thing for sure is POST request parameters are coming to this API file. Though they are not accessible in $application->request->post(), they are coming into request body.

My first issue is how should I access these POST parameters from request body and my second issue is why the request data is getting displayed into such a weird format as above after converting the request body into JSON format?

Following is the necessary code snippet:

<?php

    require 'Slim/Slim.php';    

    \Slim\Slim::registerAutoloader();

    //Instantiate Slim class in order to get a reference for the object.
    $application = new \Slim\Slim();

    $body = $application->request->getBody();
    header("Content-Type: application/json");//setting header before sending the JSON response back to the iPhone
    echo json_encode($new_body);// Converting the request body into JSON format and sending it as a response back to the iPhone. After execution of this step I'm getting the above weird format data as a response on iPhone.
    die;
?>

Upvotes: 17

Views: 59108

Answers (3)

Joel
Joel

Reputation: 854

Slim will json_decode the post body for you using this middleware

https://www.slimframework.com/docs/v4/middleware/body-parsing.html

Upvotes: 0

guillermoandrae
guillermoandrae

Reputation: 610

Generally speaking, you can access the POST parameters individually in one of two ways:

$paramValue = $application->request->params('paramName');

or

$paramValue = $application->request->post('paramName');

More info is available in the documentation: http://docs.slimframework.com/#Request-Variables

When JSON is sent in a POST, you have to access the information from the request body, for example:

$app->post('/some/path', function () use ($app) {
    $json = $app->request->getBody();
    $data = json_decode($json, true); // parse the JSON into an assoc. array
    // do other tasks
});

Upvotes: 44

lurker123456
lurker123456

Reputation: 171

"Slim can parse JSON, XML, and URL-encoded data out of the box" - http://www.slimframework.com/docs/objects/request.html under "The Request Body".

Easiest way to handle a request in any body form is via the "getParsedBody()". This will do guillermoandrae example but on 1 line instead of 2.

Example:

$allPostVars = $application->request->getParsedBody();

Then you can access any parameters by their key in the array given.

$someVariable = $allPostVars['someVariable'];

Upvotes: 15

Related Questions