Interface HttpRequestBody
public interface HttpRequestBody
Represents the body of an HTTP request.
All built-in factory methods except ofStream(InputStream, String, long) return re-readable (byte[]-backed) bodies, which makes redirect
retry safe. The ofStream(InputStream, String, long) factory is single-use.
-
Method Summary
Modifier and TypeMethodDescriptionlongdefault booleanReturnstrueif the body can be read multiple times (e.g. for retry or redirect),falseif it is a single-use stream.static HttpRequestBodyCreates a body from a byte array.static HttpRequestBodyofFormData(List<NameValuePair> params) Creates a body from URL-encoded form data.static HttpRequestBodyofStream(InputStream stream, String contentType, long contentLength) Creates a body from a streamingInputStream.static HttpRequestBodyCreates a body from a string with the given content type and charset.
-
Method Details
-
getContentType
String getContentType()- Returns:
- the MIME content type of this body, e.g.
application/x-www-form-urlencoded.
-
getContent
- Returns:
- an
InputStreamover the body content. - Throws:
IOException- if reading fails.
-
getContentLength
long getContentLength()- Returns:
- the content length in bytes, or
-1if unknown.
-
isRepeatable
default boolean isRepeatable()Returnstrueif the body can be read multiple times (e.g. for retry or redirect),falseif it is a single-use stream.Implementations created by
ofBytes(byte[], String),ofString(String, String, Charset), andofFormData(List)returntrue. Implementations created byofStream(InputStream, String, long)returnfalse.- Returns:
trueif the body is buffered and repeatable
-
ofFormData
Creates a body from URL-encoded form data.- Parameters:
params- the form parameters.- Returns:
- a re-readable
HttpRequestBody.
-
ofString
Creates a body from a string with the given content type and charset.- Parameters:
content- the string content.contentType- the MIME type.charset- the charset used to encode the string.- Returns:
- a re-readable
HttpRequestBody.
-
ofBytes
Creates a body from a byte array.- Parameters:
bytes- the content bytes.contentType- the MIME type.- Returns:
- a re-readable
HttpRequestBody.
-
ofStream
Creates a body from a streamingInputStream.Note: This body is single-use. If the HTTP implementation needs to retry (e.g., on redirect), it will fail because the stream cannot be re-read. Prefer the byte[]-backed factories for retry-safe bodies.
- Parameters:
stream- the content stream.contentType- the MIME type.contentLength- the content length in bytes, or-1if unknown.- Returns:
- a single-use
HttpRequestBody.
-