Module bus.http

Class PostBuilder


public class PostBuilder extends RequestBuilder<PostBuilder>
A builder for creating HTTP POST requests using a fluent interface. It supports setting URL, parameters, headers, a raw request body, and multipart file uploads.
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • PostBuilder

      public PostBuilder(Httpd httpd)
      Constructs a new PostBuilder.
      Parameters:
      httpd - The Httpd client instance.
  • Method Details

    • build

      public RequestCall build()
      Description copied from class: RequestBuilder
      Abstract method to be implemented by subclasses to build the final, executable request call.
      Specified by:
      build in class RequestBuilder<PostBuilder>
      Returns:
      The constructed RequestCall.
    • body

      public PostBuilder body(String body)
      Sets a raw string as the request body. This is typically used for sending content like JSON or XML. The 'Content-Type' header should be set accordingly.
      Parameters:
      body - The raw string content for the request body.
      Returns:
      This builder instance for chaining.
    • multipartBody

      public PostBuilder multipartBody(MultipartBody multipartBody)
      Sets a pre-constructed MultipartBody. This allows for advanced multipart request configuration.
      Parameters:
      multipartBody - The pre-built multipart body.
      Returns:
      This builder instance for chaining.
    • addFile

      public PostBuilder addFile(String partName, String fileName, byte[] content)
      Adds a file to the multipart request from a byte array.
      Parameters:
      partName - The name of the form field.
      fileName - The name of the file.
      content - The file content as a byte array.
      Returns:
      This builder instance for chaining.
    • addFile

      public PostBuilder addFile(String partName, String fileName, InputStream is)
      Adds a file to the multipart request from an InputStream.
      Parameters:
      partName - The name of the form field.
      fileName - The name of the file.
      is - The input stream providing the file's content.
      Returns:
      This builder instance for chaining.
    • addFile

      public PostBuilder addFile(String partName, String fileName, File file)
      Adds a file to the multipart request from a File object.
      Parameters:
      partName - The name of the form field.
      fileName - The name of the file.
      file - The file to be uploaded.
      Returns:
      This builder instance for chaining.
    • addFile

      public PostBuilder addFile(String partName, String fileName, String content) throws UnsupportedEncodingException
      Adds a file to the multipart request from a string, using the default UTF-8 encoding.
      Parameters:
      partName - The name of the form field.
      fileName - The name of the file.
      content - The string content of the file.
      Returns:
      This builder instance for chaining.
      Throws:
      UnsupportedEncodingException - if UTF-8 is not supported.
    • addFile

      public PostBuilder addFile(String partName, String fileName, String content, String charsetName) throws UnsupportedEncodingException
      Adds a file to the multipart request from a string with a specified charset.
      Parameters:
      partName - The name of the form field.
      fileName - The name of the file.
      content - The string content of the file.
      charsetName - The name of the charset to use for encoding.
      Returns:
      This builder instance for chaining.
      Throws:
      UnsupportedEncodingException - if the specified charset is not supported.
    • addFile

      public PostBuilder addFile(String partName, String fileName, byte[] content, String charsetName)
      Parameters:
      partName - The name of the form field.
      fileName - The name of the file.
      content - The file content as a byte array.
      charsetName - This parameter is ignored.
      Returns:
      This builder instance for chaining.