or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

async-operations.mdexecutor-auth.mdindex.mdrequest-operations.mdresponse-handling.md
tile.json

tessl/maven-org-apache-httpcomponents--fluent-hc

Apache HttpComponents Client fluent API providing a simplified interface for HTTP operations

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.apache.httpcomponents/fluent-hc@4.5.x

To install, run

npx @tessl/cli install tessl/maven-org-apache-httpcomponents--fluent-hc@4.5.0

index.mddocs/

Apache HttpComponents Fluent API

Apache HttpComponents Client fluent API provides a simplified and intuitive interface for making HTTP requests. It follows the fluent interface design pattern, enabling method chaining for building and executing HTTP requests with minimal boilerplate code.

Package Information

  • Package Name: fluent-hc
  • Package Type: maven
  • Language: Java
  • Group ID: org.apache.httpcomponents
  • Artifact ID: fluent-hc
  • Installation: Add to Maven pom.xml:
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>fluent-hc</artifactId>
    <version>4.5.14</version>
</dependency>

Core Imports

import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Async;

Basic Usage

import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Form;
import org.apache.http.entity.ContentType;
import java.io.IOException;

// Simple GET request
String content = Request.Get("http://example.com/api/data")
    .execute()
    .returnContent()
    .asString();

// POST request with JSON body
String response = Request.Post("http://example.com/api/users")
    .addHeader("Content-Type", "application/json")
    .bodyString("{\"name\":\"John\",\"email\":\"john@example.com\"}", ContentType.APPLICATION_JSON)
    .execute()
    .returnContent()
    .asString();

// Form POST request
String result = Request.Post("http://example.com/login")
    .bodyForm(Form.form()
        .add("username", "user")
        .add("password", "pass")
        .build())
    .execute()
    .returnContent()
    .asString();

Architecture

The fluent API is built around several key components:

  • Request: Main entry point for building HTTP requests with method chaining
  • Response: Wrapper for handling HTTP responses with multiple consumption options
  • Content: Reusable container for response content with multiple access methods
  • Executor: Context manager for multiple requests with shared authentication and cookies
  • Form: Builder for HTML form data (name-value pairs)
  • Async: Asynchronous request execution with Future-based results

Capabilities

HTTP Request Operations

Create and configure HTTP requests using fluent method chaining with support for all standard HTTP methods.

// Static factory methods for creating requests
public static Request Get(String uri);
public static Request Get(URI uri);
public static Request Post(String uri);
public static Request Post(URI uri);
public static Request Put(String uri);
public static Request Put(URI uri);
public static Request Delete(String uri);
public static Request Delete(URI uri);
public static Request Head(String uri);
public static Request Head(URI uri);
public static Request Options(String uri);
public static Request Options(URI uri);
public static Request Patch(String uri);
public static Request Patch(URI uri);
public static Request Trace(String uri);
public static Request Trace(URI uri);

HTTP Request Operations

Response Handling

Process HTTP responses using multiple consumption patterns with automatic resource management.

public class Response {
    public Content returnContent() throws ClientProtocolException, IOException;
    public HttpResponse returnResponse() throws IOException;
    public void saveContent(File file) throws IOException;
    public void discardContent();
    public <T> T handleResponse(ResponseHandler<T> handler) throws ClientProtocolException, IOException;
}

Response Handling

Authentication and Session Management

Manage authentication credentials, cookies, and connection pooling across multiple HTTP requests.

public class Executor {
    public static Executor newInstance();
    public static Executor newInstance(HttpClient httpclient);
    public Executor auth(String username, String password);
    public Executor auth(HttpHost host, String username, String password);
    public Response execute(Request request) throws ClientProtocolException, IOException;
}

Authentication and Session Management

Asynchronous Operations

Execute HTTP requests asynchronously using Future-based patterns with callback support.

public class Async {
    public static Async newInstance();
    public <T> Future<T> execute(Request request, ResponseHandler<T> handler);
    public <T> Future<T> execute(Request request, ResponseHandler<T> handler, FutureCallback<T> callback);
    public Future<Content> execute(Request request);
    public Future<Content> execute(Request request, FutureCallback<Content> callback);
}

Asynchronous Operations

Core Types

// Content container for response data
public class Content {
    public static final Content NO_CONTENT;
    public Content(byte[] raw, ContentType type);
    public ContentType getType();
    public byte[] asBytes();
    public String asString();
    public String asString(Charset charset);
    public InputStream asStream();
}

// Form builder for HTML form data
public class Form {
    public static Form form();
    public Form add(String name, String value);
    public List<NameValuePair> build();
}

// ContentResponseHandler for custom response processing
public class ContentResponseHandler extends AbstractResponseHandler<Content> {
    public ContentResponseHandler();
    public Content handleEntity(HttpEntity entity) throws IOException;
}

Form Builder Usage

The Form class provides a convenient way to build form data for POST requests:

import org.apache.http.client.fluent.Form;

// Create form with multiple fields
Form loginForm = Form.form()
    .add("username", "john.doe")
    .add("password", "secret123")
    .add("remember_me", "true");

// Use with POST request
String response = Request.Post("https://example.com/login")
    .bodyForm(loginForm.build())
    .execute()
    .returnContent()
    .asString();

// Form parameters with special characters
Form dataForm = Form.form()
    .add("query", "search term with spaces")
    .add("category", "news & events")
    .add("language", "en-US");

Exception Handling

The fluent API throws standard Java exceptions:

  • IOException - For I/O errors during request execution
  • ClientProtocolException - For HTTP protocol violations and errors
  • UnsupportedOperationException - For invalid operations (e.g., setting body on GET request)
  • IllegalStateException - For response reuse attempts after consumption

Thread Safety

  • Request objects: Not thread-safe, create new instances per thread
  • Response objects: Not thread-safe, consume only once
  • Content objects: Thread-safe and reusable
  • Executor: Thread-safe, can be shared across threads
  • Async: Thread-safe for concurrent request execution