HTTP client
MagicUtils HTTP client wraps java.net.http.HttpClient with config defaults,
JSON mapping, retries, and a small multipart helper.
Dependency
Configuration
The module stores settings in http-client.{ext} (JSONC by default on Fabric).
Key sections:
timeouts: connect + request timeoutsretry: backoff settings and retry status codeslogging: request/response logging togglesdefaults: base URL, headers, HTTP version
Basic usage
Runtime profiles
When you already have a MagicRuntime, you can declare named HTTP/WebSocket
profiles that rebuild automatically on config reload:
This removes the usual close old client -> build new client -> swap references
boilerplate from service reload paths.
Profile builder options
Every builder setter takes a Function<C, ...> of your config type, so the value
is recomputed each time the profile rebuilds on reload:
| Method | Purpose |
|---|---|
sections(String...) | Config sections the profile binds to. |
baseUrl(cfg -> ...) | Base URL. |
userAgent(cfg -> ...) | User-Agent header. |
header(name, value) / header(name, cfg -> ...) | A static or config-derived header. |
headers(cfg -> Map) | A map of headers. |
bearerAuth(cfg -> token) | Adds an Authorization: Bearer header. |
connectTimeout(cfg -> Duration) | Connection timeout. |
requestTimeout(cfg -> Duration) | Per-request timeout. |
followRedirects(cfg -> Boolean) | Redirect policy. |
version(cfg -> HttpClient.Version) | HTTP version. |
logger(cfg -> PlatformLogger) | Logger for diagnostics. |
configure(cfg, builder -> ...) / configure(builder -> ...) | Escape hatch onto the underlying MagicHttpClient.Builder. |
Profile instance methods
Once built, the profile is a small handle around the live client:
require()— the current client, or throw if not built yet.current()—Optional<MagicHttpClient>without throwing.refresh()— force a rebuild from the current config.binding()— the underlyingMagicRuntimeConfigBinding.name()— the runtime component name.close()— release the client (the runtime also closes it on shutdown).
Smart methods
Smart methods switch to async automatically on blocking-sensitive threads and
return a CompletableFuture:
JSON to POJO
JSON POST
Multipart upload
Download to file
Stream a response body straight to disk instead of into memory:
Like the other convenience methods it has downloadToFileAsync(...) and
downloadToFileSmart(...) variants.
WebSocket client
MagicWebSocketClient wraps java.net.http.WebSocket with the same builder
pattern and config integration as the HTTP client.
Basic usage
Builder options
The builder supports the same options as MagicHttpClient.Builder:
baseUrl(...)— base URL prepended to connect pathsheader(...)/headers(...)— default headersuserAgent(...)— User-Agent headerconnectTimeout(...)— connection timeoutfollowRedirects(...)— redirect policysubprotocols(...)— WebSocket subprotocol listlogger(...)— platform logger for connection diagnosticsconfig(...)/logging(...)— sharedHttpClientConfigsettingsmapper(...)— custom JacksonObjectMapper
Connect methods
| Method | Description |
|---|---|
connect(path, listener) | Synchronous connect (blocks). |
connectAsync(path, listener) | Returns CompletableFuture<WebSocket>. |
connectSmart(path, listener) | Async on blocking-sensitive threads, sync otherwise. |
Runtime profiles
Use MagicWebSocketClientProfile for config-aware WebSocket clients that
rebuild on config reload:
Cleanup
Call wsClient.close() to release resources. When using runtime profiles, the
profile handles cleanup automatically on config reload and runtime shutdown.
Async method variants
Every convenience method on MagicHttpClient has three variants:
| Suffix | Behaviour |
|---|---|
| (none) | Synchronous. Throws on blocking-sensitive threads. |
...Async(...) | Returns CompletableFuture. Always non-blocking. |
...Smart(...) | Sync when safe, async when on a blocking-sensitive thread. |
Available methods: get, getJson, post, postJson, postMultipart,
downloadToFile, send.
Notes
- Retries apply to the convenience methods (
get,post,postJson, etc.). - For raw
send(...), retries are not automatic. - JSON mapping uses Jackson; invalid JSON throws
IllegalStateException. - Synchronous methods throw
IllegalStateExceptionon blocking-sensitive threads. Use...Async()or...Smart()in handlers.