Class MultiPartBuilder

java.lang.Object
kong.unirest.core.MultiPartBuilder

public class MultiPartBuilder extends Object
A builder class for constructing multipart form data parts with customizable content type, headers, and file names.

This builder supports various value types including strings, input streams, files, and byte arrays. Use the fluent API to configure the part and then call toBodyPart() to create the final BodyPart.

Example usage:

BodyPart<?> part = MultiPartBuilder.named("file")
    .value(new File("/path/to/file.txt"))
    .contentType(ContentType.TEXT_PLAIN)
    .fileName("custom-name.txt")
    .header("X-Custom-Header", "value")
    .toBodyPart();
  • Constructor Details

    • MultiPartBuilder

      public MultiPartBuilder(String partName)
      Constructs a new MultiPartBuilder with the specified part name.
      Parameters:
      partName - the name of the multipart field
  • Method Details

    • named

      public static MultiPartBuilder named(String name)
      Creates a new MultiPartBuilder with the specified part name.
      Parameters:
      name - the name of the multipart field
      Returns:
      a new MultiPartBuilder instance
    • value

      public MultiPartBuilder value(String value)
      Sets the value of this part to a string.
      Parameters:
      value - the string value for this part
      Returns:
      this builder for method chaining
    • value

      public MultiPartBuilder value(InputStream inputStream)
      Sets the value of this part to an input stream.
      Parameters:
      inputStream - the input stream containing the part data
      Returns:
      this builder for method chaining
    • value

      public MultiPartBuilder value(File file)
      Sets the value of this part to a file.
      Parameters:
      file - the file to upload
      Returns:
      this builder for method chaining
    • value

      public MultiPartBuilder value(byte[] content)
      Sets the value of this part to a byte array.
      Parameters:
      content - the byte array containing the part data
      Returns:
      this builder for method chaining
    • value

      public MultiPartBuilder value(Object content)
      Sets the value of this part to an object. First class types supported include File, InputStream and byte[] all other types will be converted to a string
      Parameters:
      content - the object being set for a value.
      Returns:
      this builder for method chaining
    • fileName

      public MultiPartBuilder fileName(String fileName)
      Sets the file name for this part.

      This is useful when uploading input streams or byte arrays where the original file name is not available.

      Parameters:
      fileName - the file name to use for this part
      Returns:
      this builder for method chaining
    • contentType

      public MultiPartBuilder contentType(String contentType)
      Sets the content type for this part using a string representation.
      Parameters:
      contentType - the MIME type string (e.g., "application/json")
      Returns:
      this builder for method chaining
    • contentType

      public MultiPartBuilder contentType(ContentType type)
      Sets the content type for this part.
      Parameters:
      type - the content type for this part
      Returns:
      this builder for method chaining
    • header

      public MultiPartBuilder header(String name, String value)
      Adds a custom header to this part.
      Parameters:
      name - the header name
      value - the header value
      Returns:
      this builder for method chaining
    • toBodyPart

      public BodyPart<?> toBodyPart()
      Builds and returns the appropriate BodyPart based on the configured value type.

      The returned type depends on the value set:

      Returns:
      a BodyPart instance configured with the builder's settings