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:
| Method | Returns | Description |
|---|---|---|
get() | HttpResponse | Execute the request and return the full response |
statusCode() | int | Execute (if needed) and return the HTTP status code |
headers() | array | Execute (if needed) and return the response headers |
content() | string | Execute and return the response body as a string |
stream() | Generator<string> | Execute in streaming mode and yield chunks |
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, callget() 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, callstream() on the pending response. This returns a PHP Generator that yields chunks as they arrive from the server:
stream() method always forces streaming mode regardless of the request’s isStreamed() flag. Similarly, get() and content() always force synchronous mode.
Important: Callingbody()on a streamedHttpResponsethrows aLogicException. Usestream()instead. This prevents accidentally buffering a large response that was intended to be consumed incrementally.
Creating Responses Programmatically
TheHttpResponse 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:
$e->getRequest(), $e->getResponse(), and $e->getDuration() to inspect them.
Response Metadata
TheHttpResponse object also exposes metadata about the response:
withStream():