Search by

thesis / http-client

vudaltsov

Asynchronous HTTP/1 client for HTTP and HTTPS with connection pooling, streaming bodies and trailers.

dev-main / 0.1.x-dev 2026-11-23 22:55 UTC

This package is auto-updated.

Last update: 2026-11-23 23:14:52 UTC


README

Thesis HTTP Client

PHP Release License PHPStan Coverage Mutation score

This read-only repository mirrors packages/http-client. Open issues and pull requests in the thesis-php/thesis monorepo.

Report a bug · Request a feature · Changelog · Contributing

Send an HTTP/1.1 request inside a Thesis async scope. The final response head is available as soon as send() returns. Read the body under the deadline you want for the download. The client supplies a body that implements Closer. Close an unfinished body to close its connection immediately. A permanent read failure or dropping the last reference to an unfinished body also closes the connection.

use Thesis\Http\Request;
use Thesis\HttpClient\Client;
use Thesis\Http\Response;
use Uri\Rfc3986\Uri;
use function Thesis\Async\timeout;
use function Thesis\Time\sec;

$request = new Request('GET', new Uri('https://example.com/'));
$client = new Client();
$response = timeout(sec(5), static fn() => $client->send($request));
$status = $response->status;
$response->body->close();

Use withResponse() to close the body when a callback returns or throws:

$status = $client->withResponse($request, static fn(Response $response) => $response->status);

Reuse the same client across requests to share connections for the same scheme, host and port. A fixed-length body releases its socket on the last byte. A chunked body releases it after the trailers. $response->body->discard() consumes any remaining bytes. A bodyless response releases it immediately. An outstanding body keeps its own socket. The client retains one idle socket per origin, up to 32 in total. If a peer closes an idle connection, the next request opens a new one. A disconnect during a request propagates to the caller. The client never replays an upload.

HTTPS verifies the peer through thesis/net. Pass a Net ClientTls configuration to the constructor to use a private CA. The client sends the request body before reading the response. Redirects are returned to the caller.