[英]Call to undefined method GuzzleHttp\Command\Result::getBody()
I am trying to get a response in Drupal from an outside API in json format.我正在尝试从外部 API 以 json 格式获得 Drupal 的响应。 I am using HTTP Client Manager Drupal module.
我正在使用 HTTP 客户端管理器 Drupal 模块。 Right now I can only get response in stdClass Object format in Array and all the response key values are missing.
现在我只能在数组中获得 stdClass Object 格式的响应,并且所有响应键值都丢失了。
My original code:我的原始代码:
public function findPosts() {
$client = $this->getClient();
$params = array('client_Id' => "12345",
"client_Secret" => "42452454",
"scope" => "read");
$response = $client->FindPosts($params);
dpm($response);
return ['#markup' => $response];
}
Outputs the code below.输出以下代码。 What I need it too look like is [access_token] => eyJhbGciOiJIUzUxMiIsIn, [type] => bearer, etc.
我也需要它看起来像 [access_token] => eyJhbGciOiJIUzUxMiIsIn,[type] => bearer 等。
stdClass Object
(
[__CLASS__] => GuzzleHttp\Command\Result
[data:protected] => Array
(
[0] => eyJhbGciOiJIUzUxMiIsIn
[1] => bearer
[2] => 3600
[3] => 2022-11-09T10:48:47+00:00
[4] => read
[5] => MwA1ADkAZAA0AGIAZAA4AC0AOQAzADcA
[6] => 86400
[7] => 2022-11-10T09:48:47+00:00
)
)
When I try $response->getBody() or $response->getContent(), or any other response method it returns the error below.当我尝试 $response->getBody() 或 $response->getContent() 或任何其他响应方法时,它返回以下错误。
Error: Call to undefined method GuzzleHttp\Command\Result::getBody() in Drupal\http_client_manager_example\Controller\ExampleController->findPosts() (line 92 of modules/contrib/http_client_manager/modules/http_client_manager_example/src/Controller/ExampleController.php).
Drupal\http_client_manager_example\Controller\ExampleController->findPosts()
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 169)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 49)
Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
As @bwaindwain mentioned toArray()
method works instead of getBody()
.正如@bwaindwain 提到的
toArray()
方法代替getBody()
工作。
toArray()
method works wells with responses in format: toArray()
方法适用于以下格式的响应:
[
{
"token": "4354iojegdjss"
}
]
However, with this response format all keys still disappear:但是,使用这种响应格式,所有键仍然消失:
{
"token": "4354iojegdjss"
}
My fix for this issue is to manually add responseModel
in src/api/resources/posts.json
this way:我对这个问题的修复是通过这种方式在
src/api/resources/posts.json
中手动添加responseModel
:
{
"operations": {
"GetToken": {
"httpMethod": "POST",
"uri": "token",
"summary": "Get token",
},
"responseModel": "Token"
}
}
"models": {
"Token": {
"type": "object",
"location": "json",
"properties": {
"token": {
"location": "json",
"type": "string"
}
}
}
}
If somebody knows a better fix, please, comment.如果有人知道更好的解决方法,请发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.