This read-only repository mirrors
packages/http-client. Open issues and pull requests in the thesis-php/thesis monorepo.
A URL string sends a GET. Reuse the client across calls. Wrap both the request and the read in an async timeout to apply one deadline to connection acquisition, redirects and downloading the body.
use Thesis\HttpClient\Client;
use function Thesis\Async\timeout;
use function Thesis\Time\sec;
$client = new Client();
$bytes = timeout(sec(10), static fn() => $client->send('https://example.com/')->takeBody()->readAll());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->takeBody()->close();Use withResponse() to close the body when a callback returns or throws:
$status = $client->withResponse('https://example.com/', 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.
Take the body once, read until EOF to reuse the connection, then call $body->trailers() for the final fields. A bodyless response releases it immediately.
The byte limit maxResponseHeadBytes covers informational and final response headers together (default 64 KiB).
An outstanding body keeps its own socket and counts toward maxConnectionsPerOrigin (default 10).
Further callers for that origin wait for a slot. The client retains up to maxIdleConnections sockets across
all origins (default 32). Set it to zero to close each socket after its exchange.
If a peer closes an idle connection, the next request opens a new one. A disconnect during a request
propagates to the caller. Automatic retries are opt-in.
HTTPS verifies the peer through thesis/net.
Pass a Net ClientTls configuration to the constructor to use a private CA.
The transport takes and closes the request body before it reads the response.
For another send, supply a new body with withBody() or build a new request.
Pass middleware: [] for a single exchange with the original content encoding. By default the client runs
FollowRedirects then DecodeContent. Supplying a list replaces the defaults. A middleware implements
Thesis\Http\ClientMiddleware with handle(Request $request, callable $next): Response. It can return a cached response, change the request
using withHeader() or withBody(), call $next($request), or wrap the response body. Close intermediate responses before
sending another request. Middleware before FollowRedirects sees the application request once.
Middleware after it sees each destination. Scope credential injection to the intended origin.
FollowRedirects follows up to ten redirects. It resolves relative locations, turns a POST into a GET on
301/302, and drops the body on 303. For 307/308 it can resend a ReplayableBody with its original method.
It returns the intermediate response when the body cannot be replayed, the limit is reached, the location
is unusable, or HTTPS would downgrade to HTTP. Across origins only allowlisted negotiation and content
headers carry over.
DecodeContent negotiates gzip and deflate, decompresses the stream as it is read, and preserves trailers.
Set Accept-Encoding yourself or send a range request to keep control of the encoded bytes.
Place new RetryRequests(maxRetries: 1) last in the middleware list to retry an idempotent method with
a ReplayableBody on a connection, read or write failure before the response arrives. It opens an independent
reader on each retry. After send() returns, download errors propagate to the consumer.
A custom connector receives the origin host, port and optional ClientTls and returns a connected
Thesis\Net\Socket. It runs only when the pool needs a new socket. Each client owns its pool.
Form::urlEncoded() builds a POST request. Use a list of values to repeat a name.
Form::multipart() accepts ordered fields and files. Pass an unread Thesis\Io\Reader to upload a file or
forward a stream. Construction only prepares the part headers. Sending the request reads the payload in chunks.
use Thesis\HttpClient\Form;
use Thesis\HttpClient\FormPart;
$request = Form::urlEncoded('https://example.com/search', ['tag' => ['news', 'science']]);
$upload = Form::multipart('https://example.com/files', [
new FormPart('description', 'Monthly report'),
new FormPart('file', $reader, filename: 'report.pdf', contentType: 'application/pdf'),
]);
$status = $client->withResponse($upload, static fn($response) => $response->status);A string, an unread StringReader, or a Body of known size contributes its length automatically.
For another reader, pass size: to send exactly that many bytes. An unknown length selects chunked transfer.
Closing the multipart request body closes every part reader that implements Closer.
use Thesis\Http\Headers;
use Thesis\HttpClient\Client;
use Thesis\HttpClient\HttpProxy;
$proxy = new HttpProxy('proxy.internal', 3128, new Headers(['Proxy-Authorization' => $proxyCredential]));
$response = (new Client(connector: $proxy))->send('https://example.com/');The client connects to the proxy over plaintext HTTP and requests a CONNECT tunnel for the origin. For HTTPS it negotiates TLS inside the tunnel and verifies the origin's certificate.