Calling undefined method GuzzleHttp\Command\Result::getBody()
P粉426780515
P粉426780515 2023-08-29 22:08:59
0
1
444

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)

P粉426780515
P粉426780515

reply all (1)
P粉231079976

As @bwaindwainmentionedtoArray()method works instead ofgetBody().

toArray()The method works with responses in the following formats:

[ { "token": "4354iojegdjss" } ]

However, with this response format, all keys still disappear:

{ "token": "4354iojegdjss" }

My solution to this problem was to manually add theresponseModelinsrc/api/resources/posts.jsonlike this:

{ "operations": { "GetToken": { "httpMethod": "POST", "uri": "token", "summary": "Get token", }, "responseModel": "Token" } } "models": { "Token": { "type": "object", "location": "json", "properties": { "token": { "location": "json", "type": "string" } } } }

If anyone knows a better solution, please leave a comment.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!