Apache HttpComponents Client fluent API providing a simplified interface for HTTP operations
npx @tessl/cli install tessl/maven-org-apache-httpcomponents--fluent-hc@4.5.0Apache 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.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.14</version>
</dependency>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;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();The fluent API is built around several key components:
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);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;
}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
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);
}// 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;
}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");The fluent API throws standard Java exceptions:
IOException - For I/O errors during request executionClientProtocolException - For HTTP protocol violations and errorsUnsupportedOperationException - For invalid operations (e.g., setting body on GET request)IllegalStateException - For response reuse attempts after consumption