在symfony项目中,我有一个PUT方法,我尝试读取这样的数据:
$data = file_get_contents('php://input');
当我使用Postman工作时,请求在form-data
:
键:data
值:{"es_title":"edit","es_text":"text edit"}
但是当我尝试在项目中使用WebTestCase时,PUT方法中的$data
为空。
我在测试中尝试这样:
$data = array(
"data" => '{"es_title":"edit","es_text":"edit"}');
$this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey));
我也试试
$data = array(
'data' => json_encode(array(
'es_title' => 'edit',
'es_text' => 'edit'
))
);
$this->client->request('PUT', $url, $data, array(), array('HTTP_apikey' => $apikey));
如何通过测试?
答案 0 :(得分:4)
要从PUT获取数据,我在控制器中使用它:
$putData = json_decode($request->getContent(), true);
要从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)
);