I'm trying to get a response in json format from an external API in Drupal. I'm using the HTTP Client Manager Drupal module. Now I can only get the response in stdClass object format in an array and all response key values are lost.
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]; }
Output the following code. I also need it to look like [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 following error.
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 @bwaindwainmentioned
toArray()
method works instead ofgetBody()
.toArray()
The method works with responses in the following formats:However, with this response format, all keys still disappear:
My solution to this problem was to manually add the
responseModel
insrc/api/resources/posts.json
like this:If anyone knows a better solution, please leave a comment.