java.lang.Object
org.miaixz.bus.http.bodys.RequestBody
- Direct Known Subclasses:
FormBody,MultipartBody,ProgressBody
The body of an HTTP request.
This class represents the content of an HTTP request and supports creating request bodies from strings, byte arrays, files, and other sources. It provides functionality for specifying the media type, content length, and writing the content. It also supports special cases for duplex and one-shot transmission.
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionlongReturns the number of bytes that will be written tosinkwhen this request body is transmitted, or -1 if that count is unknown.abstract org.miaixz.bus.core.lang.MediaTypeReturns the media type of this request body.static RequestBodycreate(org.miaixz.bus.core.lang.MediaType contentType, byte[] content) Creates a new request body from a byte array.static RequestBodycreate(org.miaixz.bus.core.lang.MediaType contentType, byte[] content, int offset, int byteCount) Creates a new request body from a portion of a byte array.static RequestBodyCreates a new request body from a file.static RequestBodyCreates a new request body from a string.static RequestBodycreate(org.miaixz.bus.core.lang.MediaType contentType, org.miaixz.bus.core.io.ByteString content) Creates a new request body from aByteString.booleanisDuplex()Returns whether this request body is a duplex body.booleanReturns whether this request body is a one-shot body.abstract voidwriteTo(org.miaixz.bus.core.io.sink.BufferSink sink) Writes the content of this request body to the given sink.
-
Constructor Details
-
RequestBody
public RequestBody()
-
-
Method Details
-
create
Creates a new request body from a string.If
contentTypeis non-null and lacks a charset, UTF-8 will be used.- Parameters:
contentType- The media type of the content, which may be null.content- The content string.- Returns:
- A new
RequestBodyinstance.
-
create
public static RequestBody create(org.miaixz.bus.core.lang.MediaType contentType, org.miaixz.bus.core.io.ByteString content) Creates a new request body from aByteString.- Parameters:
contentType- The media type of the content, which may be null.content- The content as aByteString.- Returns:
- A new
RequestBodyinstance.
-
create
Creates a new request body from a byte array.- Parameters:
contentType- The media type of the content, which may be null.content- The content as a byte array.- Returns:
- A new
RequestBodyinstance. - Throws:
NullPointerException- if content is null.
-
create
public static RequestBody create(org.miaixz.bus.core.lang.MediaType contentType, byte[] content, int offset, int byteCount) Creates a new request body from a portion of a byte array.- Parameters:
contentType- The media type of the content, which may be null.content- The content as a byte array.offset- The starting offset in the byte array.byteCount- The number of bytes to use.- Returns:
- A new
RequestBodyinstance. - Throws:
NullPointerException- if content is null.ArrayIndexOutOfBoundsException- if the offset or byteCount are invalid.
-
create
Creates a new request body from a file.- Parameters:
contentType- The media type of the content, which may be null.file- The file to use as the content.- Returns:
- A new
RequestBodyinstance. - Throws:
NullPointerException- if file is null.
-
contentType
public abstract org.miaixz.bus.core.lang.MediaType contentType()Returns the media type of this request body.- Returns:
- The media type, which may be null.
-
contentLength
Returns the number of bytes that will be written tosinkwhen this request body is transmitted, or -1 if that count is unknown.- Returns:
- The content length.
- Throws:
IOException- if the length cannot be determined.
-
writeTo
Writes the content of this request body to the given sink.- Parameters:
sink- The sink to write to.- Throws:
IOException- if an I/O error occurs during writing.
-
isDuplex
public boolean isDuplex()Returns whether this request body is a duplex body.A duplex request body allows for interleaved transmission of request and response data, which is only supported for HTTP/2. This returns false by default unless overridden by a subclass.
- Returns:
trueif this is a duplex request body.
-
isOneShot
public boolean isOneShot()Returns whether this request body is a one-shot body.A one-shot request body can only be transmitted once, typically used for destructively-written scenarios. This returns false by default unless overridden by a subclass.
- Returns:
trueif this is a one-shot request body.
-