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

3

17

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;
?>
Sharondasharos answered 21/1, 2015 at 17:38 Comment(1)
possible duplicate of How to get the POST request entity using Slim framworkLagasse
L
44

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
});
Lagasse answered 21/1, 2015 at 17:49 Comment(6)
I tried with $application->request->params('paramName'); as well as $allPostVars = $application->request->post('paramName); but still I didn't get any POST request variable. Only after $body = $application->request->getBody(); I'm getting the POST request data.Sharondasharos
Ah I understand now. Then this is a duplicate of this: #26347460Lagasse
To expound: you'll just need to use json_decode on the request body to parse the response and get to the information.Lagasse
Thanks for your help. But as you understood my exact issue can you please post a more meaningful and descriptive answer along with corrected and modified version of my code it would be of great help to me as well as other people from PHP community?Sharondasharos
JSON isn't parsed into $_POST -- it is sent as the contents of the request body -- so it can't be accessed using the post() and params() methods. So to get to the data, you'll have to do something like: json_encode($application->request->getBody(), true) to parse the JSON string into an associative array and use as you wish.Lagasse
:Thanks once again for providing this much useful information. Why don't you replace your answer and write a new one with new code. I'll immediately accept it.Sharondasharos
T
15

"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'];
Tillie answered 12/1, 2016 at 8:18 Comment(1)
Thx for this, this is really easiest way.Tiro
W
0

Slim will json_decode the post body for you using this middleware

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

Wilhite answered 30/8, 2022 at 3:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.