In symfony project I have a PUT method and I try to read data like this:
$data = file_get_contents('php://input');
When I use Postman it works, the request is in form-data:
key: data
value: {"es_title":"edit","es_text":"text edit"}
But when I try with WebTestCase in project not works, $data in PUT method is empty.
I try like this in Test:
$data = array(
        "data" => '{"es_title":"edit","es_text":"edit"}');
$this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey));
Also I try
$data = array(
        'data' => json_encode(array(
            'es_title' => 'edit',
            'es_text' => 'edit'
        ))
    );
$this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey));
How can I do to pass the test?
To get data from a PUT I use this inside the controller:
$putData = json_decode($request->getContent(), true);
To make the request from a testCase I use this:
    $params = [
        'es_title' => 'edit',
        'es_text' => 'edit',
    ];
    $this->client->request(
        'PUT',
        $url,
        [],
        [],
        [
            'CONTENT_TYPE' => 'application/json',
            'HTTP_X-Requested-With' => 'XMLHttpRequest'
        ],
        json_encode($params)
    );
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With