Home / Modules / yoja-http-client

yoja-http-client

Fluent GET/POST builders, automatic Content-Type from the body's runtime type, a WebSocket dialer, and a shared engine that owns the connection pool — built on Vert.x Web Client.

Installation

implementation 'com.easygoingapi:yoja-http-client:VERSION'

Quick start

HttpEngine engine = new HttpEngine();
HttpClient client = HttpClient.builder(engine)
                              .host("api.example.com")
                              .port(443)
                              .ssl(true)
                              .build();

// GET with a query string
client.send(HttpGet.builder("/users")
                   .addParameter("q", "alice")
                   .build())
      .onSuccess(res -> System.out.println(res.bodyAsJsonObject()));

// POST a JSON body — Content-Type is set automatically
client.send(HttpPost.of("/users",
                        new JsonObject().put("name", "alice")))
      .onSuccess(res -> System.out.println(res.statusCode()));

Engines

HttpEngine class (AutoCloseable)

Shared engine that owns the connection pool. One engine is typically shared by every HttpClient of a given configuration.

SignatureReturnsDescription
HttpEngine()Defaults from defaultOptions() — HTTP/2 + ALPN, TLS on, trust-all
HttpEngine(WebClientOptions)Custom Vert.x options
static defaultOptions()WebClientOptionsOpinionated defaults (HTTP/2 with HTTP/1.1 ALPN fallback, TLS on, trust-all)
webClient()WebClientThe underlying Vert.x web client
options()WebClientOptions
defaultHost()String
defaultPort()int
logOptions()voidDump effective options to the log
close()voidRelease the connection pool

WebSocketEngine class

WebSocket counterpart of HttpEngine.

SignatureReturnsDescription
WebSocketEngine()Defaults from defaultOptions()
WebSocketEngine(WebSocketClientOptions)Custom Vert.x options
static defaultOptions()WebSocketClientOptionsOpinionated defaults
webSocketClient()io.vertx.core.http.WebSocketClientUnderlying Vert.x client
options()WebSocketClientOptions
defaultHost()String
defaultPort()int
logOptions()void
close()Future<Void>Async close

HttpClient

HttpClient class

Per-endpoint façade dialing a single host:port through an HttpEngine.

Factory
SignatureReturnsDescription
static builder(HttpEngine)Builder
Requests
SignatureReturnsDescription
send(HttpGet)Future<HttpResponse>Execute a GET request
send(HttpPost)Future<HttpResponse>Execute a POST; Content-Type auto-picked from the body type
Accessors
SignatureReturnsDescription
host()String
port()Integernull when the engine default is used
isSsl()boolean
timeout()Duration
Builder
SignatureReturnsDescription
host(String)BuilderFalls back to engine default when omitted
port(int)BuilderFalls back to engine default when omitted
ssl(boolean)Builder
timeout(Duration)Builder
build()HttpClient

HttpOption class

Two-knob option bundle (TLS flag + optional timeout). Defaults: SSL on, no timeout.

SignatureReturnsDescription
static builder()Builder
isSsl()boolean
timeout()Durationnull = no timeout
Builder.ssl(boolean)Builder
Builder.timeout(Duration)Builder
Builder.noTimeout()BuilderExplicitly clear any timeout
Builder.build()HttpOption

Requests & response

HttpGet class (extends HttpRequest)

GET request with optional query parameters.

Factory
SignatureReturnsDescription
static of(String path)HttpGet
static of(String path, String queryString)HttpGetWith a pre-built raw query string
static builder(String path)Builder
Builder
SignatureReturnsDescription
addParameter(name) · addParameter(name, value) · addParameter(name, List)BuilderAppend parameter (keeps duplicates)
putParameter(name) · putParameter(name, value) · putParameters(name, List)BuilderSet parameter (replaces existing)
putHeader(name, value)Builder
putCookie(name, value)Builder
build()HttpGet
Accessors
SignatureReturnsDescription
parameters()HttpParameterAll parameters as a multimap
parameters(name)List<String>All values for a given name
firstParameter(name)StringFirst value, or null
hasParameter(name)boolean
parameterNames()Set<String>
parameterSize()intTotal entry count (with duplicates)

HttpPost class (extends HttpRequest)

POST request. The body's runtime type drives the Content-Type header automatically.

Factory & Builder
SignatureReturnsDescription
static of(String path)HttpPostNo body
static of(String path, JsonObject | JsonArray | String | byte[])HttpPostWith body; Content-Type auto-detected
static builder(String path)Builder
Builder.body(JsonObject | JsonArray | String | byte[])BuilderSet the request body
Builder.putHeader(name, value) · putCookie(name, value) · build()Builder / HttpPostInherited from HttpRequest
Body accessors
SignatureReturnsDescription
bodyAsJsonObject()JsonObject
bodyAsJsonArray()JsonArray
bodyAsText()String
bodyAsBinary()byte[]
<C> body(Class<C>)CDeserialise the body to the given type

HttpRequest abstract class

Common base of HttpGet and HttpPost — path, headers, cookies.

SignatureReturnsDescription
path()String
headers()HttpHeaderAll request headers
headerNames()Set<String>
headerSize()int
hasHeader()booleantrue if any header is present
hasHeader(name)boolean
cookies()Set<HttpCookie>
cookie(name)HttpCookieFirst cookie with that name, or null
hasCookie()booleantrue if any cookie is present

HttpResponse class

Read-only view of the upstream response. Cookies are eagerly decoded.

Status
SignatureReturnsDescription
statusCode()intHTTP status code
statusMessage()StringReason phrase
version()HttpVersionHTTP/1.1 or HTTP/2
Headers & cookies
SignatureReturnsDescription
hasHeader()booleantrue if any header is present
hasHeader(name)boolean
headerNames()Set<String>
header(name)StringFirst value, or null
cookies()Set<HttpCookie>All response cookies
cookies(name)Set<HttpCookie>Cookies matching the given name
cookie(name, domain, path)HttpCookieExact cookie lookup
hasCookie()boolean
hasCookie(name)boolean
Body
SignatureReturnsDescription
bodyAsJsonObject()JsonObject
bodyAsJsonArray()JsonArray
bodyAsText()String
bodyAsBinary()byte[]
<C> body(Class<C>)CDeserialise to the given type

WebSocket client

WebSocketClient class

Connected client wrapping a Vert.x WebSocket. Built and connected through its nested builder.

Factory (Builder)
SignatureReturnsDescription
static builder(WebSocketEngine, String path)Builder
Builder.host(String)BuilderFalls back to engine default
Builder.port(int)BuilderFalls back to engine default
Builder.ssl(boolean)Builder
Builder.timeout(Duration)Builder
Builder.connect()Future<WebSocketClient>Dial and resolve when the socket is open
Outgoing
SignatureReturnsDescription
send(String)Future<Void>Send a text frame
send(byte[])Future<Void>Send a binary frame
close()Future<Void>Close the WebSocket connection
Handlers
SignatureReturnsDescription
onTextMessage(Handler<TextMessageEvent>)voidFires on each incoming text frame; event holds message()
onBinaryMessage(Handler<BinaryMessageEvent>)voidFires on each incoming binary frame; event holds message()
onClose(Handler<CloseEvent>)voidFires when the socket is closed; event holds code() and reason()
Accessors
SignatureReturnsDescription
getPath()StringThe path this socket was opened on

HttpException runtime exception

Wraps any I/O or build failure on a request. Extends YojaAppException.

Module README: yoja-http-client/README.md.