Skip to main content

1. Build a Client

use Cognesy\Http\HttpClient;

$client = HttpClient::default(); // default preset (curl)
// @doctest id="6b0e"
Use a specific driver preset when needed:
$client = HttpClient::using('guzzle');
// @doctest id="557f"

2. Create and Execute a Request

use Cognesy\Http\Data\HttpRequest;

$request = new HttpRequest(
    url: 'https://api.example.com/health',
    method: 'GET',
    headers: ['Accept' => 'application/json'],
    body: '',
    options: [],
);

$response = $client->withRequest($request)->get();

echo $response->statusCode();
echo $response->body();
// @doctest id="4807"

3. Add Middleware (Immutable)

$client = $client->withMiddleware($middleware);
// @doctest id="4ba1"
Do not rely on in-place mutation.

4. Fast Local Testing with Mock Driver

use Cognesy\Http\Creation\HttpClientBuilder;
use Cognesy\Http\Data\HttpResponse;

$client = (new HttpClientBuilder())
    ->withMock(function ($mock) {
        $mock->addResponse(
            HttpResponse::sync(200, ['Content-Type' => 'application/json'], '{"ok":true}'),
            url: 'https://api.example.com/health',
            method: 'GET'
        );
    })
    ->create();
// @doctest id="eb26"
This is the recommended default for deterministic examples and tests.