Koper
Koper

Reputation: 123

Symfony 2 set Request content and retrieve it

I am working in Symfony2 and I want to se the content of a Request with a JSON string and use i.e.: $request->get('name') to access the content.

JSON string:

$string = '{
            "name":"Bob",
            "surname":"White",
            "email":"[email protected]",
            "nationality":"",
        }';

$request = new Request ($query = array(), $request = array(), $attributes = array(), $cookies = array(), $files = array(), $server = array(), $content = $string);

var_dump($request->get('name'));die;

To me the above is a valid way but the var dump gives me null... can anyone see where I m going wrong here...?

Upvotes: 1

Views: 1038

Answers (1)

OIS
OIS

Reputation: 10033

You want something like this?

use Symfony\Component\HttpFoundation\Request;

$input = '{
            "name":"Bob",
            "surname":"White",
            "email":"[email protected]",
            "nationality":""
        }';
$data = json_decode($input, true);


$request = new Request (array(), $data);

var_dump($request->request->get('name'));

die;

Upvotes: 4

Related Questions