Learn how to handle HTTP responses using the Instructor HTTP client API.
HttpResponse
interface, which provides a uniform way to access response data:
HttpClient::handle()
method, it returns an implementation of HttpResponse
:
PsrHttpResponse
: Used by the GuzzleDriverSymfonyHttpResponse
: Used by the SymfonyDriverLaravelHttpResponse
: Used by the LaravelDriverMockHttpResponse
: Used by the MockHttpDriver for testingstatusCode()
method:
headers()
method:
Content-Type
), while others might use lowercase (e.g., content-type
).
body()
method:
json_decode()
function:
RequestException
, which is thrown when a request fails:
RequestException
often wraps another exception from the underlying HTTP client, which you can access with $e->getPrevious()
.
failOnError
configuration option:
failOnError
is set to true
, the client will throw a RequestException
for error responses. When it’s false
, you need to check the status code yourself: