VOOZH about

URL: https://deepwiki.com/hypervel/http-client/3-quick-start-guide

⇱ Quick Start Guide | hypervel/http-client | DeepWiki


Loading...
Menu

Quick Start Guide

This guide demonstrates how to make basic HTTP requests with the Hypervel HTTP Client. Each section includes practical examples showing request execution and response handling. For installation instructions, see Installation and Setup. For comprehensive configuration options, see Making HTTP Requests and Core Components.

Creating a Client Instance

The Factory class is the entry point for making HTTP requests. Create a Factory instance to begin:


Factory Request Flow


Sources: src/Factory.php93-96 src/Factory.php518-525 src/Factory.php404-409 src/Factory.php414-417

The Factory constructor accepts an optional EventDispatcherInterface parameter. When provided, the client dispatches events at key lifecycle points (see Event System). Without a dispatcher, requests execute without event hooks.

Making GET Requests

The Factory provides HTTP verb methods that create and execute requests. Call get() to send a GET request:


GET Request Execution


Sources: src/Factory.php518-525 src/Factory.php404-409 src/PendingRequest.php187-210 src/PendingRequest.php624-637 src/PendingRequest.php713-794 src/PendingRequest.php1033-1036

Query Parameters

Pass query parameters as the second argument to get():


Query Parameter Processing


Sources: src/PendingRequest.php624-637 src/PendingRequest.php713-794 src/PendingRequest.php807-833

The withQueryParameters() method provides an alternative way to add query parameters:


Sources: src/PendingRequest.php304-311

Making POST Requests

The post() method sends data in the request body. By default, data is sent as JSON with Content-Type: application/json:


Sources: src/PendingRequest.php660-665 src/PendingRequest.php187-210 src/PendingRequest.php239-242

Request Body Formats

The client supports multiple body formats, controlled by the bodyFormat property:

Body FormatMethodContent-TypeUse Case
jsonasJson()application/jsonAPI requests (default)
form_paramsasForm()application/x-www-form-urlencodedForm submissions
multipartasMultipart()multipart/form-dataFile uploads
bodywithBody()CustomRaw body content

Sources: src/PendingRequest.php239-242 src/PendingRequest.php247-250 src/PendingRequest.php286-289 src/PendingRequest.php225-234

Form Data Example


File Upload Example


The attach() method automatically switches to multipart format.

Sources: src/PendingRequest.php257-281

Handling Responses

Request methods return a Response object that wraps PSR-7 ResponseInterface with convenience methods:

Response Methods


Sources: src/Response.php61-64 src/Response.php69-80 src/Response.php173-176 src/Response.php165-168 src/Response.php197-200 src/Response.php129-132 src/Response.php137-140

Accessing Response Data

Raw Body


JSON Decoding


Sources: src/Response.php61-64 src/Response.php69-80

Collection Wrapper

The collect() method wraps JSON data in a Collection instance for fluent data manipulation:


Sources: src/Response.php129-132

Fluent Object

The fluent() method returns a Fluent object for property-style access:


Sources: src/Response.php137-140

Status Code Checking

The Response class provides helper methods for checking HTTP status codes:

MethodStatus RangeDescription
successful()200-2992xx success responses
redirect()300-3993xx redirect responses
clientError()400-4994xx client error responses
serverError()500+5xx server error responses
failed()400+Any error response

Sources: src/Response.php197-200 src/Response.php205-208 src/Response.php221-224 src/Response.php229-232 src/Response.php213-216

Error Handling

By default, the client does not throw exceptions on HTTP errors. Check response status or use exception methods:

Status Checking


Sources: src/Response.php197-200 src/Response.php213-216

Exception Methods

Configure the request to throw exceptions on errors:


Sources: src/PendingRequest.php566-571 src/Response.php297-310 src/Response.php329-337 src/Response.php370-373

Available Exception Methods

MethodCondition
throw()Throws RequestException if failed (4xx or 5xx)
throwIf($condition)Throws if condition evaluates to true
throwIfStatus($code)Throws if status matches specific code
throwIfClientError()Throws on 4xx status
throwIfServerError()Throws on 5xx status

Sources: src/Response.php297-310 src/Response.php317-320 src/Response.php329-337 src/Response.php360-363 src/Response.php370-373

For retry logic and advanced error handling, see Error Handling and Exceptions and Retry Logic and Error Recovery.

Request Configuration

The fluent API allows chaining configuration methods before sending requests:


Configuration Method Chain


Sources: src/PendingRequest.php342-349 src/PendingRequest.php392-397 src/PendingRequest.php476-481 src/PendingRequest.php496-508 src/PendingRequest.php660-665

Common Configuration Methods

MethodPurposeExample
withHeaders()Add request headerswithHeaders(['Accept' => 'application/json'])
withToken()Bearer authenticationwithToken('api-token')
withBasicAuth()HTTP Basic authwithBasicAuth('user', 'pass')
timeout()Request timeout (seconds)timeout(30)
retry()Retry configurationretry(3, 100)
withOptions()Guzzle-compatible optionswithOptions(['verify' => false])

Sources: src/PendingRequest.php342-349 src/PendingRequest.php392-397 src/PendingRequest.php372-377 src/PendingRequest.php476-481 src/PendingRequest.php496-508 src/PendingRequest.php513-521

For complete configuration reference, see Request Configuration.

Complete Request Flow

The following diagram shows the complete journey from method call to response:


Sources: src/Factory.php518-525 src/Factory.php404-409 src/Factory.php414-417 src/PendingRequest.php660-665 src/PendingRequest.php713-794 src/PendingRequest.php807-833 src/PendingRequest.php1059-1078 src/PendingRequest.php953-972 src/PendingRequest.php1207-1210 src/PendingRequest.php1023-1030