Home  >  Q&A  >  body text

Calling undefined method GuzzleHttp\Command\Result::getBody()

<p>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. </p> <p>My original code: </p> <pre class="brush:php;toolbar:false;">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]; }</pre> <p>Output the following code. I also need it to look like [access_token] => eyJhbGciOiJIUzUxMiIsIn, [type] => bearer etc. </p> <pre class="brush:php;toolbar:false;">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 ) )</pre> <p>When I try $response->getBody() or $response->getContent() or any other response method, it returns the following error.</p> <pre class="brush:php;toolbar:false;">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)</pre></p>
P粉426780515P粉426780515386 days ago472

reply all(1)I'll reply

  • P粉231079976

    P粉2310799762023-08-30 18:59:05

    As @bwaindwain mentioned toArray() method works instead of getBody().

    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 the responseModel in src/api/resources/posts.json like 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.

    reply
    0
  • Cancelreply