Skip to main content
Every HTTP request is represented by an HttpRequest value object. You construct it with explicit parameters — URL, method, headers, body, and options — and pass it to HttpClient::send(). There are no magic methods or implicit state; what you see is what gets sent.

Request Structure

The HttpRequest constructor accepts five named parameters:
Each request is also assigned a unique id and timestamped with createdAt and updatedAt properties automatically.

GET Requests

GET requests are the simplest form. Pass query parameters directly in the URL:

POST with JSON Body

When you pass an array as the body, it is automatically JSON-encoded through the HttpRequestBody class:
You can also pass a pre-encoded JSON string if you need precise control over the encoding. String bodies are sent as-is; the drivers do not parse and reserialize them:

PUT, PATCH, and DELETE

All HTTP methods work the same way. Just change the method string:

Setting Headers

Headers are passed as a flat associative array. Common headers include authentication tokens, content types, and custom application headers:

Modifying Requests

HttpRequest is immutable. The with*() methods return a new instance with the modification applied:
You can read request properties at any time through accessor methods:
The body is managed by HttpRequestBody, which provides toString() and toArray() conversions:

Streaming Option

To enable streaming on a request, either set the stream option in the constructor or use withStreaming():
When isStreamed() returns true, calling stream() on the PendingHttpResponse will yield chunks as they arrive instead of buffering the entire response.

Building Clients with Config

For production use, you will typically configure the client with typed options:
This gives you a client that uses Guzzle, connects within 5 seconds, times out after 30 seconds, and throws exceptions on 4xx/5xx responses. See Changing Client Config for the full list of options.