Skip to content

Catch a Guzzle Exception and Read the HTTP Body

When a Guzzle request fails, the exception may still carry an HTTP response.

OUTPUT FORMATSHTMLJSONMarkdown

Catching RequestException lets you inspect whether a response exists and read its body before deciding how to recover.

Why RequestException is the practical catch point

RequestException is a higher-level exception that covers BadResponseException, TooManyRedirectsException, and several related exceptions. Catching it gives your code one place to decide whether the failure was an HTTP error with a body or a transport-level problem without a response.

Exception hierarchy at a glance

The hierarchy shows how Guzzle exceptions relate. TransferException implements GuzzleException. ConnectException implements NetworkExceptionInterface. RequestException sits above BadResponseException and TooManyRedirectsException. BadResponseException branches into ServerException and ClientException.

Decision checklist before reading the body

  1. Wrap the request in a try block. 2. Catch RequestException. 3. Call $e->hasResponse() to test whether a response is attached. 4. If true, call $e->getResponse() and then $response->getBody(). 5. If false, handle a network or other error without a body.

Worked example using the captured flow

The example below follows the captured PHP flow: create a Client, call get() on a URL, and read the body on success. In the catch block, the code checks hasResponse(). When a response exists, it retrieves it with getResponse() and reads the body. When no response exists, it falls into the network-error branch. The code is supplied as a protected module and should be treated as the exact contract.

Choosing a more specific exception

If you want to be more specific about the exceptions you catch, use ClientException for 400-level errors, ServerException for 500-level errors, or BadResponseException for both. These are supplied as exact class names in the protected module.

Failure distinction: HTTP error vs transport failure

An HTTP error such as a 404 or similar status can still include a response body. A transport failure such as a connection problem may not include any response. The hasResponse() check separates these two cases so you do not attempt to read a body that does not exist.

Related Guzzle questions

Explore related Guzzle topics such as HTTP basic authentication, ignoring SSL certificate errors, whether Guzzle uses cURL, handling client errors, sending JSON POST requests, proxy authentication, whether Guzzle is built into PHP, deprecation status, and what Guzzle is used for in PHP.

Frequently asked questions

How do I get the HTTP body from a Guzzle exception?

Catch RequestException, check $e->hasResponse(), then call $e->getResponse()->getBody() when a response exists.

What if the exception has no response?

If hasResponse() returns false, there is no HTTP body to read. Handle the network or other error in that branch.

Which exception should I catch for 400-level errors?

Use GuzzleHttp\Exception\ClientException for 400-level errors.

Which exception should I catch for 500-level errors?

Use GuzzleHttp\Exception\ServerException for 500-level errors.

Can I catch both 400-level and 500-level errors together?

Yes. GuzzleHttp\Exception\BadResponseException covers both.