Class HttpRequest

java.lang.Object
org.eclipse.rdf4j.http.client.spi.HttpRequest

public class HttpRequest extends Object
An HTTP request with method, URI, headers, and optional body.

Instances are created via the HttpRequest.Builder, which is obtained from newBuilder(String, URI). For common request patterns, consider using the convenience factory methods in HttpRequests.

Headers can be manipulated after construction via addHeader(String, String), setHeader(String, String), setHeaders(Map), unsetHeader(String), and unsetHeaders(), which allows AuthenticationHandler implementations to modify requests in-place.

Example usage:

HttpRequest request = HttpRequest.newBuilder("POST", URI.create("https://example.com/sparql"))
                .header("Accept", "application/sparql-results+json")
                .body(HttpRequestBody.ofFormData(params))
                .build();
See Also:
  • Method Details

    • getMethod

      public String getMethod()
      Returns the HTTP method of this request (e.g. GET, POST).
      Returns:
      the HTTP method; never null
    • getUri

      public URI getUri()
      Returns the target URI of this request.
      Returns:
      the request URI; never null
    • getHeaders

      public List<HttpHeader> getHeaders()
      Returns the list of headers associated with this request. The list may be empty if no headers were set.
      Returns:
      the list of HttpHeaders; never null
    • addHeader

      public void addHeader(String name, String value)
      Appends a header to this request. Multiple calls with the same name are additive; no deduplication is performed.
      Parameters:
      name - the header name; must not be null
      value - the header value; must not be null
    • setHeader

      public void setHeader(String name, String value)
      Replaces all existing values for the given header name with a single new value. The comparison is case-insensitive.
      Parameters:
      name - the header name; must not be null
      value - the new header value; must not be null
    • setHeaders

      public void setHeaders(Map<String,String> headerMap)
      Replaces all existing headers with the entries from the given map. Each map entry sets a single value for its header name, replacing any previously set values for that name. Header names that are not present in the map are left untouched.
      Parameters:
      headerMap - a map of header names to values; must not be null
    • unsetHeader

      public void unsetHeader(String name)
      Removes all headers with the given name. The comparison is case-insensitive.
      Parameters:
      name - the header name to remove; must not be null
    • unsetHeaders

      public void unsetHeaders()
      Removes all headers from this request.
    • getBody

      public Optional<HttpRequestBody> getBody()
      Returns the optional request body. For methods that do not carry a body (e.g. GET, DELETE) this will be empty.
      Returns:
      an Optional containing the HttpRequestBody, or empty if no body was set
    • getResponseTimeout

      public Optional<Duration> getResponseTimeout()
      Returns the optional response timeout for this request. When present, the HTTP client must abort the request if no response data arrives within this duration. Overrides any client-level socket timeout for this specific request.
      Returns:
      an Optional containing the response timeout, or empty if no per-request timeout is set
    • newBuilder

      public static HttpRequest.Builder newBuilder(String method, URI uri)
      Creates a new HttpRequest.Builder for the given HTTP method and target URI.
      Parameters:
      method - the HTTP method (e.g. "GET", "POST"); must not be null
      uri - the target URI; must not be null
      Returns:
      a new HttpRequest.Builder
    • copyOf

      public static HttpRequest.Builder copyOf(HttpRequest original, URI uri)
      Creates a new HttpRequest.Builder pre-populated from an existing HttpRequest, optionally targeting a different URI. The method, all headers, and the body are copied from original; the URI is replaced by uri.
      Parameters:
      original - the request to copy from; must not be null
      uri - the target URI for the new request; must not be null
      Returns:
      a new HttpRequest.Builder seeded with the original's method, headers, and body