Mastering mica-http: Fluent HTTP Requests with OkHttp Wrapper

mica-http is a Java wrapper for OkHttp that offers a fluent‑style HTTP client, showing Maven/Gradle integration, global logging configuration, synchronous and asynchronous request examples, and advanced features like header, cookie, query, form handling, and response processing with Jackson and Jsoup.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Mastering mica-http: Fluent HTTP Requests with OkHttp Wrapper

mica-http

mica-http

is a wrapper of okhttp, providing a fluent‑style HTTP utility whose syntax follows the HttpClient Fluent API.

Usage

Maven

<dependency>
  <groupId>net.dreamlu</groupId>
  <artifactId>mica-http</artifactId>
  <version>${version}</version>
</dependency>

Gradle

compile("net.dreamlu:mica-http:${version}")

Documentation

Set the global log level (NONE, BASIC, HEADERS, BODY). Default is NONE. HttpRequest.setGlobalLog(LogLevel.BODY); Synchronously request a URL; supported methods: GET, POST, PATCH, PUT, DELETE.

HttpRequest.get("https://www.baidu.com")
    .log(LogLevel.BASIC)               // request‑level log level
    .addHeader("x-account-id", "mica001") // add header
    .addCookie(new Cookie.Builder()
        .name("sid")
        .value("mica_user_001")
        .build())
    .query("q", "mica")                 // URL parameter, auto‑encoded
    .queryEncoded("name", "encodedValue")
    .formBuilder()
    .add("id", 123123)                  // form parameter
    .execute()
    .asJsonNode();                       // result conversion (throws on network error)

Example of synchronous POST with failure handling:

String html = HttpRequest.post("https://www.baidu.com")
    .execute()
    .onFailed((request, e) -> {
        e.printStackTrace();
    })
    .onResponse(ResponseSpec::asString);

Example of synchronous PATCH with success handling:

String text = HttpRequest.patch("https://www.baidu.com")
    .execute()
    .onSuccess(ResponseSpec::asString);

Asynchronous request example (DELETE):

HttpRequest.delete("https://www.baidu.com")
    .async()
    .onFailed((request, e) -> {
        e.printStackTrace();
    })
    .onResponse(responseSpec -> {
        int httpCode = responseSpec.code();
    })
    .onSuccessful(responseSpec -> {
        JsonNode jsonNode = responseSpec.asJsonNode();
    })
    .execute();

Example Code 1

// Set global log level
HttpRequest.setGlobalLog(LogLevel.BODY);

// Use Jackson JSON‑Path syntax
private String getUserEmail(String accessToken) {
    return HttpRequest.get("https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))")
        .addHeader("Host", "api.linkedin.com")
        .addHeader("Connection", "Keep-Alive")
        .addHeader("Authorization", "Bearer " + accessToken)
        .execute()
        .asJsonNode()
        .at("/elements/0/handle~0/emailAddress")
        .asText();
}

// Asynchronous example
public static void test() {
    HttpRequest.post("https://www.baidu.com/do-stuff")
        .log(LogLevel.BASIC)
        .formBuilder()
        .add("a", "b")
        .async()
        .onSuccessful(System.out::println)
        .onFailed((request, e) -> {
            e.printStackTrace();
        })
        .execute();
}

Example Code 2

HttpRequest.setGlobalLog(LogLevel.BODY);

// Synchronous request, returns null on exception
String html = HttpRequest.get("www.baidu.com")
    .connectTimeout(Duration.ofSeconds(1000))
    .query("test", "a")
    .query("name", "張三")
    .query("x", 1)
    .query("abd", Base64Util.encode("123&$#%"))
    .queryEncoded("abc", Base64Util.encode("123&$#%"))
    .execute()
    .onFailed((request, e) -> {
        e.printStackTrace();
    })
    .onSuccess(ResponseSpec::asString);
System.out.println(html);

// Synchronous call returning Optional, empty on exception
Optional<String> opt = HttpRequest.post(URI.create("https://www.baidu.com"))
    .bodyString("Important stuff")
    .formBuilder()
    .add("a", "b")
    .execute()
    .onSuccessOpt(ResponseSpec::asString);

// Synchronous request with response consumption
HttpRequest.post("https://www.baidu.com/some-form")
    .addHeader("X-Custom-header", "stuff")
    .execute()
    .onSuccessful(responseSpec -> {
        String text = responseSpec.asString();
        System.out.println(text);
    });

// Synchronous request that throws on error
HttpRequest.get("https://www.baidu.com/some-form")
    .execute()
    .asString();

// Asynchronous request with result handling
HttpRequest.get("https://www.baidu.com/some-form")
    .async()
    .onSuccessful(System.out::println)
    .onFailed((request, e) -> {
        e.printStackTrace();
    })
    .execute();
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaHTTPOkHttpFluent API
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.