I am trying to get a response in Drupal from an outside API in json format. I am using HTTP Client Manager Drupal module. Right now I can only get response in stdClass Object format in Array and all the response key values are missing.
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.
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.
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()
.
toArray()
method works wells with responses in format:
[
{
"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:
{
"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.