Class HttpRequest
java.lang.Object
org.eclipse.rdf4j.http.client.spi.HttpRequest
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:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidAppends a header to this request.static HttpRequest.BuildercopyOf(HttpRequest original, URI uri) Creates a newHttpRequest.Builderpre-populated from an existingHttpRequest, optionally targeting a different URI.getBody()Returns the optional request body.Returns the list of headers associated with this request.Returns the HTTP method of this request (e.g.Returns the optional response timeout for this request.getUri()Returns the target URI of this request.static HttpRequest.BuildernewBuilder(String method, URI uri) Creates a newHttpRequest.Builderfor the given HTTP method and target URI.voidReplaces all existing values for the given header name with a single new value.voidsetHeaders(Map<String, String> headerMap) Replaces all existing headers with the entries from the given map.voidunsetHeader(String name) Removes all headers with the given name.voidRemoves all headers from this request.
-
Method Details
-
getMethod
Returns the HTTP method of this request (e.g.GET,POST).- Returns:
- the HTTP method; never
null
-
getUri
-
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; nevernull
-
addHeader
-
setHeader
-
setHeaders
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 benull
-
unsetHeader
Removes all headers with the given name. The comparison is case-insensitive.- Parameters:
name- the header name to remove; must not benull
-
unsetHeaders
public void unsetHeaders()Removes all headers from this request. -
getBody
Returns the optional request body. For methods that do not carry a body (e.g.GET,DELETE) this will be empty.- Returns:
- an
Optionalcontaining theHttpRequestBody, or empty if no body was set
-
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
Optionalcontaining the response timeout, or empty if no per-request timeout is set
-
newBuilder
Creates a newHttpRequest.Builderfor the given HTTP method and target URI.- Parameters:
method- the HTTP method (e.g."GET","POST"); must not benulluri- the target URI; must not benull- Returns:
- a new
HttpRequest.Builder
-
copyOf
Creates a newHttpRequest.Builderpre-populated from an existingHttpRequest, optionally targeting a different URI. The method, all headers, and the body are copied fromoriginal; the URI is replaced byuri.- Parameters:
original- the request to copy from; must not benulluri- the target URI for the new request; must not benull- Returns:
- a new
HttpRequest.Builderseeded with the original's method, headers, and body
-