Class HttpBodies

java.lang.Object
org.apache.juneau.http.HttpBodies

public final class HttpBodies extends Object
Static factory methods for creating common HTTP body types.

Import statically for clean DSL-style usage:

import static HttpBodies.*; RestRequest req = client.post("/api/upload") .body(stringBody("hello world", "text/plain"));

See Also:
Since:
9.2.1
  • Method Details

    • stringBody

      public static StringBody stringBody(String content)
      Creates a UTF-8 text/plain body.
      Parameters:
      content - The string content. Must not be null.
      Returns:
      A new body. Never null.
    • stringBody

      public static StringBody stringBody(String content, String contentType)
      Creates a string body with a specific content type.
      Parameters:
      content - The string content. Must not be null.
      contentType - The MIME content type. May be null.
      Returns:
      A new body. Never null.
    • byteArrayBody

      public static ByteArrayBody byteArrayBody(byte[] bytes)
      Creates a body backed by a byte array.
      Parameters:
      bytes - The byte content. Must not be null.
      Returns:
      A new body. Never null.
    • byteArrayBody

      public static ByteArrayBody byteArrayBody(byte[] bytes, String contentType)
      Creates a body backed by a byte array with a specific content type.
      Parameters:
      bytes - The byte content. Must not be null.
      contentType - The MIME content type. May be null.
      Returns:
      A new body. Never null.
    • fileBody

      public static FileBody fileBody(File file)
      Creates a body backed by a file.
      Parameters:
      file - The file. Must not be null.
      Returns:
      A new body. Never null.
    • fileBody

      public static FileBody fileBody(File file, String contentType)
      Creates a body backed by a file with a specific content type.
      Parameters:
      file - The file. Must not be null.
      contentType - The MIME content type. May be null.
      Returns:
      A new body. Never null.
    • streamBody

      public static StreamBody streamBody(InputStream stream)
      Creates a streaming body backed by an InputStream.
      Parameters:
      stream - The input stream. Must not be null.
      Returns:
      A new body. Never null.
    • streamBody

      public static StreamBody streamBody(InputStream stream, String contentType)
      Creates a streaming body backed by an InputStream with a specific content type.
      Parameters:
      stream - The input stream. Must not be null.
      contentType - The MIME content type. May be null.
      Returns:
      A new body. Never null.
    • formBody

      public static PartList formBody(HttpPart... parts)
      Creates a URL-encoded form body from the given parts.
      Parameters:
      parts - The form parts. Must not be null.
      Returns:
      A new body. Never null.
    • formBodyOfPairs

      public static PartList formBodyOfPairs(String... pairs)
      Creates a URL-encoded form body from alternating name/value string pairs.
      Parameters:
      pairs - Alternating name/value strings. Must not be null. Length must be even.
      Returns:
      A new body. Never null.