I have a Drupal controller method that does a post using Guzzle. When I use postman, the response is valid json, but when the controller returns the data, it's missing the first curly brace. Am I doing anything obviously wrong?
public function getproject(Request $request)
{
$client = \Drupal::httpClient();
$request = $client->post($this->uri, [
'json' => [
'projectOrJobNumber'=> $request->query->get('projectNo'),
'accessCode' => $request->query->get('accessCode')
]
]);
$response = $request->getBody(true);
\Drupal::logger('my_module')->notice($response);
return $response;
}
The log shows the string missing the opening curly brace/bracket. Is there an easy way to fix this?
In the log
Message @"jobs":[{"Address":"Master Services","Contacts":[{"Jo
Try to replace
$request->getBody(true)
with
$request->getBody()->getContents()
I don't know if Drupal wraps Guzzle somehow, but out of the box Guzzle doesn't modify the response. So you should be fine, just use ->getContents()
on the body or convert it to (string)
.