Module bus.http

Class HttpRequest

java.lang.Object
org.miaixz.bus.http.plugin.httpz.HttpRequest
Direct Known Subclasses:
DeleteRequest, GetRequest, HeadRequest, PostRequest, PutRequest

public abstract class HttpRequest extends Object
An abstract base class for representing an HTTP request. It encapsulates common request properties such as URL, parameters, headers, and body, and provides a template for building the final Httpd Request object.
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • id

      protected String id
      The unique identifier for this request.
    • url

      protected String url
      The request URL.
    • body

      protected String body
      The request body as a raw string (e.g., for JSON).
    • params

      protected Map<String,String> params
      The request parameters (e.g., for form submissions or query strings).
    • encodedParams

      protected Map<String,String> encodedParams
      The pre-encoded request parameters.
    • headers

      protected Map<String,String> headers
      The request headers.
    • multipartBody

      protected MultipartBody multipartBody
      The multipart body for file uploads.
    • list

      protected List<MultipartFile> list
      A list of files for multipart uploads.
    • builder

      protected Request.Builder builder
      The builder for the final Httpd Request.
  • Constructor Details

    • HttpRequest

      protected HttpRequest(String url, Object tag, Map<String,String> params, Map<String,String> headers, List<MultipartFile> list, String body, MultipartBody multipartBody, String id)
      Constructs an HttpRequest.
      Parameters:
      url - The request URL.
      tag - A tag for the request, used for cancellation.
      params - The request parameters.
      headers - The request headers.
      list - A list of files for multipart uploads.
      body - The raw request body string.
      multipartBody - The multipart body.
      id - The unique request identifier.
    • HttpRequest

      protected HttpRequest(String url, Object tag, Map<String,String> params, Map<String,String> encodedParams, Map<String,String> headers, List<MultipartFile> list, String body, MultipartBody multipartBody, String id)
      Constructs an HttpRequest with both standard and pre-encoded parameters.
      Parameters:
      url - The request URL.
      tag - A tag for the request, used for cancellation.
      params - The standard request parameters.
      encodedParams - The pre-encoded request parameters.
      headers - The request headers.
      list - A list of files for multipart uploads.
      body - The raw request body string.
      multipartBody - The multipart body.
      id - The unique request identifier.
  • Method Details

    • createRequestBody

      public static RequestBody createRequestBody(org.miaixz.bus.core.lang.MediaType contentType, InputStream is)
      A static factory method to create a RequestBody from an InputStream.
      Parameters:
      contentType - The media type of the content.
      is - The input stream to read from.
      Returns:
      A new RequestBody instance.
      Throws:
      NullPointerException - if the input stream is null.
    • buildRequestBody

      protected abstract RequestBody buildRequestBody()
      Abstract method to be implemented by subclasses to construct the specific request body (e.g., for POST, PUT).
      Returns:
      The constructed RequestBody.
    • buildRequest

      protected abstract Request buildRequest(RequestBody requestBody)
      Abstract method to be implemented by subclasses to construct the final Request object with the correct HTTP method.
      Parameters:
      requestBody - The request body to be used.
      Returns:
      The final Request object.
    • build

      public RequestCall build(Httpd httpd)
      Builds a RequestCall, which is the executable representation of this request.
      Parameters:
      httpd - The Httpd client instance.
      Returns:
      A new RequestCall.
    • createRequest

      public Request createRequest(Callback callback)
      Creates the final Request object. This is a convenience method that combines building the request body and the request itself.
      Parameters:
      callback - The callback for the request (not used in this implementation).
      Returns:
      The constructed Request.
    • headers

      protected void headers()
      Appends the configured headers to the internal Request.Builder.
    • getId

      public String getId()
      Gets the unique identifier for this request.
      Returns:
      The request ID string.