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

Cant get any POSTed JSON data?

Scott's Avatar

Scott

02 Dec, 2013 05:15 AM

I'm trying to get Slim setup and working, and am using Postman (in Chrome) to test Slim calls but cant figure out how to get any of the posted JSON data.

Im submitting raw JSON:

{name":"John Smith", "age":"30", "gender

With Content-Type: application/json in the header

via POST to:

domain/api/v1/users/post/test/

Every attempt to get the JSON data gives a fatal error (see code comments below)

<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim(); $app->add(new \Slim\Middleware\ContentTypes());

$app->group('/api/v1', function () use ($app) {

$app->group('/users/post', function () use ($app) {

    $app->post('/test/', function () {

            print_r($app->request->headers); //no errors, but no output?

            echo "Hello!"; // outputs as expected

            $data = $app->request()->params('name'); //Fatal error: Call to a member function request() on a non-object
            $data = $app->request->getBody(); //Fatal error: Call to a member function getBody() on a non-object
            $data = $app->request->post('name'); //Fatal error: Call to a member function post() on a non-object
            $data = $app->request()->post(); //Fatal error: Call to a member function request() on a non-object

    });

});

});

$app->run(); ?>

Status is 200. What am I doing wrong? :0

Thanks!

  1. 1 Posted by Scott on 02 Dec, 2013 05:50 AM

    Scott's Avatar

    Sorry, but as was pointed out by someone on another forum, the problem ended up being an issue with how I was interpreting the docs.

    $app->post('/test/', function () {

    Should be:

    $app->post('/test/', function () use ($app) {

    My bad. Carry on!

  2. 2 Posted by mvinoth94 on 11 Sep, 2015 07:57 PM

    mvinoth94's Avatar

    Its not about the post function. Its about how to read the json request from the client.
    For your info, there is no problem when using the application/x-www-form-urlencoded. Only facing the issue when handle/read the json request.
    i am also facing the same issue by reading json request.

    $request = $app->request();
        $body    = $request->getBody();
        $input   = array(json_decode($body,true));
    

    this doesn't help at all.
    Why not post example of the code how to read json request. It will be a pleasure for us.
    Thanks.

  3. 3 Posted by rdfgr on 08 May, 2017 09:44 AM

    rdfgr's Avatar

    edwedwqdqw

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