Class MultiPartBuilder
java.lang.Object
kong.unirest.core.MultiPartBuilder
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 Summary
ConstructorsConstructorDescriptionMultiPartBuilder(String partName) Constructs a new MultiPartBuilder with the specified part name. -
Method Summary
Modifier and TypeMethodDescriptioncontentType(String contentType) Sets the content type for this part using a string representation.contentType(ContentType type) Sets the content type for this part.Sets the file name for this part.Adds a custom header to this part.static MultiPartBuilderCreates a new MultiPartBuilder with the specified part name.BodyPart<?> Builds and returns the appropriateBodyPartbased on the configured value type.value(byte[] content) Sets the value of this part to a byte array.Sets the value of this part to a file.value(InputStream inputStream) Sets the value of this part to an input stream.Sets the value of this part to an object.Sets the value of this part to a string.
-
Constructor Details
-
MultiPartBuilder
Constructs a new MultiPartBuilder with the specified part name.- Parameters:
partName- the name of the multipart field
-
-
Method Details
-
named
Creates a new MultiPartBuilder with the specified part name.- Parameters:
name- the name of the multipart field- Returns:
- a new MultiPartBuilder instance
-
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
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
Sets the value of this part to a file.- Parameters:
file- the file to upload- Returns:
- this builder for method chaining
-
value
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
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
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
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
Sets the content type for this part.- Parameters:
type- the content type for this part- Returns:
- this builder for method chaining
-
header
Adds a custom header to this part.- Parameters:
name- the header namevalue- the header value- Returns:
- this builder for method chaining
-
toBodyPart
Builds and returns the appropriateBodyPartbased on the configured value type.The returned type depends on the value set:
InputStream→InputStreamPartFile→FilePartbyte[]→ByteArrayPart- Other (including String) →
ParamPart
- Returns:
- a BodyPart instance configured with the builder's settings
-