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 Details

    • getContentType

      String getContentType()
      Returns:
      the MIME content type of this body, e.g. application/x-www-form-urlencoded.
    • getContent

      InputStream getContent() throws IOException
      Returns:
      an InputStream over the body content.
      Throws:
      IOException - if reading fails.
    • getContentLength

      long getContentLength()
      Returns:
      the content length in bytes, or -1 if unknown.
    • isRepeatable

      default boolean isRepeatable()
      Returns true if the body can be read multiple times (e.g. for retry or redirect), false if it is a single-use stream.

      Implementations created by ofBytes(byte[], String), ofString(String, String, Charset), and ofFormData(List) return true. Implementations created by ofStream(InputStream, String, long) return false.

      Returns:
      true if the body is buffered and repeatable
    • ofFormData

      static HttpRequestBody ofFormData(List<NameValuePair> params)
      Creates a body from URL-encoded form data.
      Parameters:
      params - the form parameters.
      Returns:
      a re-readable HttpRequestBody.
    • ofString

      static HttpRequestBody ofString(String content, String contentType, Charset charset)
      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

      static HttpRequestBody ofBytes(byte[] bytes, String contentType)
      Creates a body from a byte array.
      Parameters:
      bytes - the content bytes.
      contentType - the MIME type.
      Returns:
      a re-readable HttpRequestBody.
    • ofStream

      static HttpRequestBody ofStream(InputStream stream, String contentType, long contentLength)
      Creates a body from a streaming InputStream.

      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 -1 if unknown.
      Returns:
      a single-use HttpRequestBody.