Module bus.http

Class Request.Builder

java.lang.Object
org.miaixz.bus.http.Request.Builder
Enclosing class:
Request

public static class Request.Builder extends Object
A builder for creating and modifying Request instances.
  • Constructor Details

    • Builder

      public Builder()
      Default constructor that initializes a GET request.
  • Method Details

    • url

      public Request.Builder url(UnoUrl url)
      Sets the URL for this request.
      Parameters:
      url - The UnoUrl object.
      Returns:
      this builder instance.
      Throws:
      NullPointerException - if url is null.
    • url

      public Request.Builder url(String url)
      Sets the URL for this request from a string.

      This converts WebSocket URLs (ws: or wss:) to HTTP URLs (http: or https:).

      Parameters:
      url - The URL string.
      Returns:
      this builder instance.
      Throws:
      NullPointerException - if url is null.
      IllegalArgumentException - if the URL is invalid.
    • url

      public Request.Builder url(URL url)
      Sets the URL for this request from a URL object.
      Parameters:
      url - The URL object.
      Returns:
      this builder instance.
      Throws:
      NullPointerException - if url is null.
      IllegalArgumentException - if the URL scheme is not http or https.
    • header

      public Request.Builder header(String name, String value)
      Sets a header, replacing any existing headers with the same name.
      Parameters:
      name - The header name.
      value - The header value.
      Returns:
      this builder instance.
    • addHeader

      public Request.Builder addHeader(String name, String value)
      Adds a header, preserving any existing headers with the same name.

      For certain headers (like Content-Length, Content-Encoding), the HTTP client may replace the value based on the request body.

      Parameters:
      name - The header name.
      value - The header value.
      Returns:
      this builder instance.
    • removeHeader

      public Request.Builder removeHeader(String name)
      Removes all headers with the given name.
      Parameters:
      name - The header name.
      Returns:
      this builder instance.
    • headers

      public Request.Builder headers(Headers headers)
      Sets all headers, replacing any existing headers.
      Parameters:
      headers - The Headers object.
      Returns:
      this builder instance.
    • cacheControl

      public Request.Builder cacheControl(CacheControl cacheControl)
      Sets the cache control header.

      This replaces any existing Cache-Control header. If cacheControl has no directives, the Cache-Control header will be removed.

      Parameters:
      cacheControl - The CacheControl object.
      Returns:
      this builder instance.
    • get

      public Request.Builder get()
      Sets this request to be a GET request.
      Returns:
      this builder instance.
    • head

      public Request.Builder head()
      Sets this request to be a HEAD request.
      Returns:
      this builder instance.
    • post

      public Request.Builder post(RequestBody body)
      Sets this request to be a POST request.
      Parameters:
      body - The request body.
      Returns:
      this builder instance.
    • delete

      public Request.Builder delete(RequestBody body)
      Sets this request to be a DELETE request with a request body.
      Parameters:
      body - The request body.
      Returns:
      this builder instance.
    • delete

      public Request.Builder delete()
      Sets this request to be a DELETE request with no request body.
      Returns:
      this builder instance.
    • put

      public Request.Builder put(RequestBody body)
      Sets this request to be a PUT request.
      Parameters:
      body - The request body.
      Returns:
      this builder instance.
    • patch

      public Request.Builder patch(RequestBody body)
      Sets this request to be a PATCH request.
      Parameters:
      body - The request body.
      Returns:
      this builder instance.
    • method

      public Request.Builder method(String method, RequestBody body)
      Sets the HTTP method and request body.
      Parameters:
      method - The HTTP method.
      body - The request body, which may be null.
      Returns:
      this builder instance.
      Throws:
      NullPointerException - if method is null.
      IllegalArgumentException - if method is empty or if the body is not compatible with the method.
    • tag

      public Request.Builder tag(Object tag)
      Attaches a tag to this request using Object.class as the key.
      Parameters:
      tag - The tag object.
      Returns:
      this builder instance.
    • tag

      public <T> Request.Builder tag(Class<? super T> type, T tag)
      Attaches a tag to this request using the specified type as the key.

      Tags can be used to attach metadata for debugging, timing, or other purposes, and can be read in interceptors, event listeners, or callbacks. Use null to remove an existing tag of the specified type.

      Type Parameters:
      T - The type of the tag value.
      Parameters:
      type - The type of the tag.
      tag - The tag value, which may be null.
      Returns:
      this builder instance.
      Throws:
      NullPointerException - if type is null.
    • build

      public Request build()
      Builds a new Request instance.
      Returns:
      A new Request object.
      Throws:
      IllegalStateException - if the URL is not set.