I have noticed in the latest version when I get a http response code(lets say 400) from the url I am hitting guzzle will mask the original exception and just send back 400. Previously I have able to use$e->getResponse()->json() in order to get the json response from the api i was hitting. Is there a way to stop guzzle from masking the exceptions and return the original content? I know there is an option to turn off exceptions but I do not want to turn off exceptions I just want the original message rather than just "400 bad response" from guzzle.
This is all still supported (except the ->json() function). Can you provide a code sample of what you are trying to do?
sure... below I am hitting an api to get a list of subnets. if I send an invalid section that subnets are in I get an error from the api in json format. it would send a 400 response back with some json saying what went wrong. The problem is that guzzle masks this json and all I see is "400 client error".
$url = $this->baseUrl . "subnet?section=$section";
try {
$response = $this->client->get($url);
$subnets = json_decode($response->getBody()->getContents(), true);
return $subnets['subnets'];
} catch (\Exception $ex) {
$response = $ex->getResponse();
\Log::critical($ex);
dd($response);
}
that $response variable is an object and does not contain the json returned from my api.
is there a "getContents()" type function to get this json response?
The exception should be an instance of BadResponseException which has a getResponse method. You can then cast the response body to a string.
use GuzzleHttp\Exception\BadResponseException;
$url = $this->baseUrl . "subnet?section=$section";
try {
$response = $this->client->get($url);
$subnets = json_decode($response->getBody(), true);
return $subnets['subnets'];
} catch (BadResponseException $ex) {
$response = $ex->getResponse();
$jsonBody = (string) $response->getBody();
// do something with json string...
}
ahh I did not realize the getResponse() method was the same as the response returned from the client call.
this works for what I am doing....
$response = json_decode($ex->getResponse()->getBody()->getContents(), true);
now I have the json response as an array.
thank you!
$ex->getResponse()->getBody(true) returns the contents too
I'm using Guzzle version 6.x, and I can't get return of an exception as array.
I've been try @warroyo method, but response said null.
Here is my code.
try {
$clientAPI = route('api-admin-doctor-submit-registration');
$client = new \GuzzleHttp\Client();
$request = $client->request('POST', $clientAPI, [
'form_params' => $request->all()
]);
$APIData = json_decode($request->getBody()->getContents(), true);
$data = $APIData['data'];
return response()->json($data);
} catch (\GuzzleHttp\Exception\BadResponseException $exception) {
return response()->json([
'status' => 'error',
'code' => 500,
'message' => 'Oops, looks like something went wrong',
'errors' => json_decode($exception->getResponse()->getBody()->getContents(), true)
]);
}
My point is how to make this response as an array, so I can read it as json.
But, value on array key errors is null.
Please give me a solution. Thanks.
Most helpful comment
ahh I did not realize the getResponse() method was the same as the response returned from the client call.
this works for what I am doing....
now I have the json response as an array.
thank you!