to be clear guzzle is not handling custom HTTP status messages, and you're not even actually sending one, per say, all your sending is a status code, and in your request body some content that may or may not contain a message.
bases on the status code guzzle's throwing an exception. the ...->getMessage() is literally returning what guzzle sets on the exception when it throws it.
All guzzle is making is an exception message. but at no point are you actually using a custom HTTP status message.
you can access the underlying guzzle PSR HTTP response just by calling $request->toPsrResponse() as all the Http client is, is a wrapper to make guzzle easier to use.
and equally, you can retrieve your custom error message from the body payload just like guzzle does something like
if($request->failed()){
echo $request->json('message');
}
// "Doh! we're closed"
// assuming the response was something like
return response()->json([
'message' => "Doh! we're closed",
], 400);
but if you still want to use exceptions you can just chain on ->throw() to the request.
try {
$client = new \GuzzleHttp\Client();
$reponse = $client->get('http://127.0.0.1:8000/api/exception');
} catch (\Throwable $th) {
dd($th->getMessage());
}
// Client error: `GET http://127.0.0.1:8000/api/exception` resulted in a `400 Bad Request` response:\n
// {"message":"Doh! we're closed"}\n
try {
$request = Http::get('http://127.0.0.1:8000/api/exception')
->throw();
} catch (\Throwable $th) {
dd($th->getMessage());
}
// HTTP request returned status code 400:\n
// {"message":"Doh! we're closed"}\n