CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-httpcomponents--httpmime

Apache HttpComponents HttpClient MIME multipart entity support for handling multipart/form-data requests and file uploads

Pending
Overview
Eval results
Files

multipart-entity-building.mddocs/

Multipart Entity Building

Core functionality for creating multipart HTTP entities using the MultipartEntityBuilder fluent API. This is the primary entry point for building multipart/form-data requests with support for various content types, encoding modes, and streaming operations.

Capabilities

MultipartEntityBuilder Factory

Creates new builder instances for constructing multipart entities.

/**
 * Creates a new MultipartEntityBuilder instance
 * @return New builder instance for method chaining
 */
public static MultipartEntityBuilder create();

Multipart Mode Configuration

Controls the multipart encoding standard and compatibility mode used for the generated entity.

/**
 * Set the multipart mode for encoding compliance
 * @param mode The multipart mode (STRICT, BROWSER_COMPATIBLE, or RFC6532)
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder setMode(HttpMultipartMode mode);

/**
 * Set browser-compatible mode (shortcut for BROWSER_COMPATIBLE mode)
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder setLaxMode();

/**
 * Set strict RFC-compliant mode (shortcut for STRICT mode)
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder setStrictMode();

Boundary and Content Type Configuration

Customize the multipart boundary and content type settings for the entity.

/**
 * Set a custom boundary string for the multipart entity
 * @param boundary Custom boundary string
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder setBoundary(String boundary);

/**
 * Set the MIME subtype for the multipart entity
 * @param subType MIME subtype (e.g., "form-data", "mixed")
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder setMimeSubtype(String subType);

/**
 * Set the complete content type for the multipart entity
 * @param contentType ContentType object with full type information
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder setContentType(ContentType contentType);

/**
 * Set the character encoding for the multipart entity
 * @param charset Character encoding to use
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder setCharset(Charset charset);

Adding Text Content

Add text fields to the multipart form with various content type options.

/**
 * Add a text field with default text/plain content type
 * @param name Field name for the form part
 * @param text Text content
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addTextBody(String name, String text);

/**
 * Add a text field with specified content type
 * @param name Field name for the form part
 * @param text Text content
 * @param contentType Content type for the text (e.g., text/html, application/json)
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addTextBody(String name, String text, ContentType contentType);

Adding Binary Content

Add binary data from various sources including files, byte arrays, and input streams.

/**
 * Add binary content from a byte array with default binary content type
 * @param name Field name for the form part
 * @param b Byte array containing the binary data
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addBinaryBody(String name, byte[] b);

/**
 * Add binary content from a byte array with specified content type and filename
 * @param name Field name for the form part
 * @param b Byte array containing the binary data
 * @param contentType Content type for the binary data
 * @param filename Filename to use in Content-Disposition header
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addBinaryBody(String name, byte[] b, ContentType contentType, String filename);

/**
 * Add binary content from a file with default binary content type
 * @param name Field name for the form part
 * @param file File to upload
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addBinaryBody(String name, File file);

/**
 * Add binary content from a file with specified content type and filename
 * @param name Field name for the form part
 * @param file File to upload
 * @param contentType Content type for the file
 * @param filename Custom filename to use in Content-Disposition header
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addBinaryBody(String name, File file, ContentType contentType, String filename);

/**
 * Add binary content from an input stream with default binary content type
 * @param name Field name for the form part
 * @param stream Input stream containing the binary data
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addBinaryBody(String name, InputStream stream);

/**
 * Add binary content from an input stream with specified content type and filename
 * @param name Field name for the form part
 * @param stream Input stream containing the binary data
 * @param contentType Content type for the stream data
 * @param filename Filename to use in Content-Disposition header
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addBinaryBody(String name, InputStream stream, ContentType contentType, String filename);

Adding Custom Parts

Add pre-built form parts with custom headers and configuration.

/**
 * Add a custom form body part with full control over headers and content
 * @param bodyPart Pre-configured FormBodyPart instance
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addPart(FormBodyPart bodyPart);

/**
 * Add a part with a custom content body implementation
 * @param name Field name for the form part
 * @param contentBody Custom ContentBody implementation
 * @return This builder instance for method chaining
 */
public MultipartEntityBuilder addPart(String name, ContentBody contentBody);

Building the Entity

Create the final HttpEntity from the configured multipart builder.

/**
 * Build and return the configured multipart HttpEntity
 * @return HttpEntity suitable for use with HttpClient requests
 */
public HttpEntity build();

Usage Examples:

import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.ContentType;
import java.io.File;
import java.io.FileInputStream;

// Basic form submission
HttpEntity basicForm = MultipartEntityBuilder.create()
    .addTextBody("username", "johndoe")
    .addTextBody("email", "john@example.com")
    .addTextBody("message", "Hello server!")
    .build();

// File upload with metadata
HttpEntity fileUpload = MultipartEntityBuilder.create()
    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
    .addBinaryBody("document", new File("report.pdf"), 
                   ContentType.APPLICATION_PDF, "monthly-report.pdf")
    .addTextBody("category", "reports")
    .addTextBody("confidential", "true")
    .build();

// Mixed content with streams and custom boundary
HttpEntity mixedContent = MultipartEntityBuilder.create()
    .setBoundary("MyCustomBoundary123")
    .setCharset(StandardCharsets.UTF_8)
    .addTextBody("description", "Profile data", ContentType.TEXT_PLAIN)
    .addBinaryBody("avatar", new FileInputStream("avatar.jpg"), 
                   ContentType.IMAGE_JPEG, "avatar.jpg")
    .addBinaryBody("resume", resumeBytes, ContentType.APPLICATION_PDF, "resume.pdf")
    .build();

// JSON data with file
HttpEntity jsonWithFile = MultipartEntityBuilder.create()
    .addTextBody("metadata", "{\"type\":\"document\",\"version\":1}", 
                 ContentType.APPLICATION_JSON)
    .addBinaryBody("file", document, ContentType.APPLICATION_OCTET_STREAM, "data.bin")
    .build();

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-httpcomponents--httpmime

docs

content-body-types.md

form-part-customization.md

index.md

multipart-entity-building.md

tile.json