如何传递参数以删除 guzzle 中的请求
how to I pass parameter to delete request in guzzle
我使用 guzzle 作为 http 客户端来测试我的 symfony api。
文档中提供了 url 选项,但是我如何传递 userid 和 api id 参数以便删除特定用户的特定记录。
当我用c测试时url
curl -i -X DELETE http://localhost/us/serenify/web/app_dev.php/userapi/delete/1/6
我的 api 工作正常,显示了适当的响应。
但我无法使用 guzzle 对其进行测试,因为我无法找到传递参数的选项。
这是一个定义和执行 Symfony 路由的示例:
{
"operations": {
"deleteEntity": {
"httpMethod": "DELETE",
"uri": "/userapi/delete/{userid}/{apiid}",
"summary": "Deletes an entity",
"parameters": {
"userid": {
"location": "query"
},
"apiid": {
"location": "query"
}
}
}
}
}
和代码:
class MyApi
{
protected $client;
public function __construct(ClientInterface $client, $baseUrl)
{
$this->client = $client;
//tell the client what the base URL to use for the request
$this->client->setBaseUrl($baseUrl);
//fill the client with all the routes
$description = ServiceDescription::factory("/path/to/routes.json");
$this->client->setDescription($description);
}
public function deleteEntity($userId, $apiId)
{
$params = array(
'userid' => $userId,
'apiid' => $apiId
);
$command = $this->client->getCommand('deleteEntity', $params);
$command->prepare();
$response = $this->client->execute($command);
return $response;
}
}
$client = new Guzzle\Service\Client();
$api = new MyApi($client, ' http://localhost/us/serenify/web/app_dev.php');
$api->deleteEntity(1, 6);
现在,就目前而言,生成的路线看起来像
http://localhost/us/serenify/web/app_dev.php/userapi/delete?userid=1&apiid=6.
如果您不希望 Guzzle 将参数作为查询参数传递,而是像 URL 参数那样传递,您所要做的就是在 JSON 定义文件中将它们的类型从 查询到uri.
PS:我没有测试上面的代码。可能开箱即用,也可能不会。可能需要进行细微调整。
我使用 guzzle 作为 http 客户端来测试我的 symfony api。
文档中提供了 url 选项,但是我如何传递 userid 和 api id 参数以便删除特定用户的特定记录。
当我用c测试时url
curl -i -X DELETE http://localhost/us/serenify/web/app_dev.php/userapi/delete/1/6
我的 api 工作正常,显示了适当的响应。
但我无法使用 guzzle 对其进行测试,因为我无法找到传递参数的选项。
这是一个定义和执行 Symfony 路由的示例:
{
"operations": {
"deleteEntity": {
"httpMethod": "DELETE",
"uri": "/userapi/delete/{userid}/{apiid}",
"summary": "Deletes an entity",
"parameters": {
"userid": {
"location": "query"
},
"apiid": {
"location": "query"
}
}
}
}
}
和代码:
class MyApi
{
protected $client;
public function __construct(ClientInterface $client, $baseUrl)
{
$this->client = $client;
//tell the client what the base URL to use for the request
$this->client->setBaseUrl($baseUrl);
//fill the client with all the routes
$description = ServiceDescription::factory("/path/to/routes.json");
$this->client->setDescription($description);
}
public function deleteEntity($userId, $apiId)
{
$params = array(
'userid' => $userId,
'apiid' => $apiId
);
$command = $this->client->getCommand('deleteEntity', $params);
$command->prepare();
$response = $this->client->execute($command);
return $response;
}
}
$client = new Guzzle\Service\Client();
$api = new MyApi($client, ' http://localhost/us/serenify/web/app_dev.php');
$api->deleteEntity(1, 6);
现在,就目前而言,生成的路线看起来像
http://localhost/us/serenify/web/app_dev.php/userapi/delete?userid=1&apiid=6.
如果您不希望 Guzzle 将参数作为查询参数传递,而是像 URL 参数那样传递,您所要做的就是在 JSON 定义文件中将它们的类型从 查询到uri.
PS:我没有测试上面的代码。可能开箱即用,也可能不会。可能需要进行细微调整。