Skip to main content
When you call HttpClient::send(), it returns a PendingHttpResponse. This object is lazy — no HTTP call is made until you explicitly request the response data. This design lets you decide whether to consume the response as a buffered body or as a stream of chunks.

The Pending Response

PendingHttpResponse provides several methods for reading the response:
The pending response caches its result internally. Calling get() multiple times will not send the request again. Streaming and synchronous execution are cached independently to avoid mode collisions — you can safely call both content() and stream() on the same pending response.

Buffered Responses

For a standard request, call get() to receive the full HttpResponse:

Decoding JSON

Most APIs return JSON. Decode the body with PHP’s built-in function:

Checking Status Codes

You can inspect the status code to branch on success or failure:

Streamed Responses

For streamed requests, call stream() on the pending response. This returns a PHP Generator that yields chunks as they arrive from the server:
The stream() method always forces streaming mode regardless of the request’s isStreamed() flag. Similarly, get() and content() always force synchronous mode.
Important: Calling body() on a streamed HttpResponse throws a LogicException. Use stream() instead. This prevents accidentally buffering a large response that was intended to be consumed incrementally.

Creating Responses Programmatically

The HttpResponse class provides factory methods for creating responses in tests or middleware:

Error Handling with failOnError

By default, failOnError is false and 4xx/5xx responses are returned normally. When you set it to true in the config, the client throws typed exceptions:
Each exception carries the original request, the response (if available), and the duration of the call. Use $e->getRequest(), $e->getResponse(), and $e->getDuration() to inspect them.

Response Metadata

The HttpResponse object also exposes metadata about the response:
You can create a new response with a replaced stream using withStream():
This is the primary mechanism used by middleware to intercept and transform streamed data.