在symfony'php:// input'中测试PUT为空

xim*_*o12 5 php put request symfony

在symfony项目中,我有一个PUT方法,我尝试读取这样的数据:

$data = file_get_contents('php://input');
Run Code Online (Sandbox Code Playgroud)

当我使用Postman它工作时,请求是form-data:

键: data

值: {"es_title":"edit","es_text":"text edit"}

但是当我在项目中尝试使用WebTestCase时,$dataPUT方法是空的.我在Test中尝试这样:

$data = array(
        "data" => '{"es_title":"edit","es_text":"edit"}');
$this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey));
Run Code Online (Sandbox Code Playgroud)

我也试试

$data = array(
        'data' => json_encode(array(
            'es_title' => 'edit',
            'es_text' => 'edit'
        ))
    );

$this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey));
Run Code Online (Sandbox Code Playgroud)

如何通过测试?

Ale*_*eri 6

要从 PUT 获取数据,我在控制器中使用它:

$putData = json_decode($request->getContent(), true);
Run Code Online (Sandbox Code Playgroud)

要从 testCase 发出请求,我使用以下命令:

    $params = [
        'es_title' => 'edit',
        'es_text' => 'edit',
    ];

    $this->client->request(
        'PUT',
        $url,
        [],
        [],
        [
            'CONTENT_TYPE' => 'application/json',
            'HTTP_X-Requested-With' => 'XMLHttpRequest'
        ],
        json_encode($params)
    );
Run Code Online (Sandbox Code Playgroud)