Interface Request
- All Known Implementing Classes:
HttpRequest
,TunnelRequest
Request
represents an HTTP request, and offers a fluent interface to customize
various attributes such as the path, the headers, the content, etc.
You can create Request
objects via HttpClient.newRequest(String)
and
you can send them using either send()
for a blocking semantic, or
send(Response.CompleteListener)
for an asynchronous semantic.
Request
objects cannot be reused for other requests, not even for requests
that have identical URI and headers.
- See Also:
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Listener for the request begin event.static interface
Listener for the request committed event.static interface
A client-side representation of HTTP request content.static interface
Listener for the request content event.static interface
Listener for the request failed event.static interface
Listener for the request headers event.static interface
Listener for all request events.static interface
Listener for the request queued event.static interface
Common, empty, super-interface for request listeners.static interface
Listener for the request succeeded event. -
Method Summary
Modifier and TypeMethodDescriptionAttempts to abort the send of this request.body
(Request.Content content) cookie
(HttpCookie cookie) Shortcut method to specify a file as a content for this request, with the default content type of "application/octet-stream".Shortcut method to specify a file as a content for this request, with the given content type.followRedirects
(boolean follow) getAgent()
getBody()
default Connection
Returns the connection associated with this request.getHost()
long
getPath()
int
getPort()
getQuery()
getTag()
long
default Transport
getURI()
headers
(Consumer<HttpFields.Mutable> consumer) Modifies the headers of this request.default Request
idleTimeout
(long timeout, TimeUnit unit) boolean
listener
(Request.Listener listener) method
(HttpMethod method) onComplete
(Response.CompleteListener listener) onPush
(BiFunction<Request, Request, Response.CompleteListener> pushHandler) Sets a handler for pushed resources.onRequestBegin
(Request.BeginListener listener) onRequestCommit
(Request.CommitListener listener) onRequestContent
(Request.ContentListener listener) onRequestFailure
(Request.FailureListener listener) onRequestHeaders
(Request.HeadersListener listener) onRequestQueued
(Request.QueuedListener listener) onRequestSuccess
(Request.SuccessListener listener) onResponseBegin
(Response.BeginListener listener) onResponseContent
(Response.ContentListener listener) onResponseFailure
(Response.FailureListener listener) onResponseHeader
(Response.HeaderListener listener) onResponseHeaders
(Response.HeadersListener listener) Registers a listener for the headers event.onResponseSuccess
(Response.SuccessListener listener) Adds a URI query parameter with the given name and value.Specifies the URI path - and possibly the query - of this request.default Request
port
(int port) send()
Sends this request and returns the response.void
send
(Response.CompleteListener listener) Sends this request and asynchronously notifies the given listener for response events.Tags this request with the given metadata tag.trailersSupplier
(Supplier<HttpFields> trailers) Provides the trailers of this request.default Request
version
(HttpVersion version)
-
Method Details
-
getConnection
Returns the connection associated with this request.
The connection is available only starting from the
request begin
event, when a connection is associated with the request to be sent, otherwisenull
is returned.- Returns:
- the connection associated with this request,
or
null
if there is no connection associated with this request
-
getScheme
String getScheme()- Returns:
- the URI scheme of this request, such as "http" or "https"
-
scheme
- Parameters:
scheme
- the URI scheme of this request, such as "http" or "https"- Returns:
- this request object
-
getHost
String getHost()- Returns:
- the URI host of this request, such as "127.0.0.1" or "google.com"
-
host
- Parameters:
host
- the URI host of this request, such as "127.0.0.1" or "google.com"- Returns:
- this request object
-
getPort
int getPort()- Returns:
- the URI port of this request such as 80 or 443
-
port
- Parameters:
port
- the URI port of this request such as 80 or 443- Returns:
- this request object
-
transport
- Parameters:
transport
- theTransport
of this request- Returns:
- this request object
-
getTransport
- Returns:
- the
Transport
of this request
-
getMethod
String getMethod()- Returns:
- the method of this request, such as GET or POST, as a String
-
method
- Parameters:
method
- the method of this request, such as GET or POST- Returns:
- this request object
-
method
- Parameters:
method
- the method of this request, such as GET or POST- Returns:
- this request object
-
getPath
String getPath()- Returns:
- the URI path of this request, such as "/" or "/path" - without the query
- See Also:
-
path
Specifies the URI path - and possibly the query - of this request. If the query part is specified, parameter values must be properlyUTF-8 URL encoded
. For example, if the value for parameter "currency" is the euro symbol € then the query string for this parameter must be "currency=%E2%82%AC". For transparent encoding of parameter values, useparam(String, String)
.- Parameters:
path
- the URI path of this request, such as "/" or "/path?param=1"- Returns:
- this request object
-
getQuery
String getQuery()- Returns:
- the URI query string of this request such as "param=1"
- See Also:
-
getURI
URI getURI()- Returns:
- the full URI of this request such as
https://host:port/path?param=1
-
getVersion
HttpVersion getVersion()- Returns:
- the HTTP version of this request, such as "HTTP/1.1"
-
version
- Parameters:
version
- the HTTP version of this request, such as "HTTP/1.1"- Returns:
- this request object
-
getParams
Fields getParams()- Returns:
- the URI query parameters of this request
-
param
Adds a URI query parameter with the given name and value. The value isUTF-8 URL encoded
.- Parameters:
name
- the name of the query parametervalue
- the value of the query parameter- Returns:
- this request object
-
getHeaders
HttpFields getHeaders()- Returns:
- the headers of this request
-
headers
Modifies the headers of this request.- Parameters:
consumer
- the code that modifies the headers of this request- Returns:
- this request object
-
getTrailersSupplier
Supplier<HttpFields> getTrailersSupplier()- Returns:
- the supplier of the trailers of this request
-
trailersSupplier
Provides the trailers of this request.- Parameters:
trailers
- the supplier of the trailers of this request- Returns:
- this request object
-
getCookies
List<HttpCookie> getCookies()- Returns:
- the cookies associated with this request
-
cookie
- Parameters:
cookie
- a cookie for this request- Returns:
- this request object
-
tag
Tags this request with the given metadata tag.
Each different tag will create a different destination, even if the destination origin is the same.
This is particularly useful in proxies, where requests for the same origin but from different clients may be tagged with client's metadata (e.g. the client remote address).
The tag metadata class must correctly implement
Object.hashCode()
andObject.equals(Object)
so that it can be used, along with the origin, to identify a destination.- Parameters:
tag
- the metadata to tag the request with- Returns:
- this request object
-
getTag
Object getTag()- Returns:
- the metadata this request has been tagged with
-
attribute
- Parameters:
name
- the name of the attributevalue
- the value of the attribute- Returns:
- this request object
-
getAttributes
- Returns:
- the attributes of this request
-
getBody
Request.Content getBody()- Returns:
- the request content of this request
-
body
- Parameters:
content
- the request content of this request- Returns:
- this request object
-
file
Shortcut method to specify a file as a content for this request, with the default content type of "application/octet-stream".- Parameters:
file
- the file to upload- Returns:
- this request object
- Throws:
IOException
- if the file does not exist or cannot be read
-
file
Shortcut method to specify a file as a content for this request, with the given content type.- Parameters:
file
- the file to uploadcontentType
- the content type of the file- Returns:
- this request object
- Throws:
IOException
- if the file does not exist or cannot be read
-
getAgent
String getAgent()- Returns:
- the user agent for this request
-
agent
- Parameters:
agent
- the user agent for this request (corresponds to theUser-Agent
header)- Returns:
- this request object
-
accept
- Parameters:
accepts
- the media types that are acceptable in the response, such as "text/plain;q=0.5" or "text/html" (corresponds to theAccept
header)- Returns:
- this request object
-
getIdleTimeout
long getIdleTimeout()- Returns:
- the idle timeout for this request, in milliseconds
-
idleTimeout
- Parameters:
timeout
- the idle timeout for this requestunit
- the idle timeout unit- Returns:
- this request object
-
getTimeout
long getTimeout()- Returns:
- the total timeout for this request, in milliseconds; zero or negative if the timeout is disabled
-
timeout
- Parameters:
timeout
- the total timeout for the request/response conversation; use zero or a negative value to disable the timeoutunit
- the timeout unit- Returns:
- this request object
-
isFollowRedirects
boolean isFollowRedirects()- Returns:
- whether this request follows redirects
-
followRedirects
- Parameters:
follow
- whether this request follows redirects- Returns:
- this request object
-
listener
- Parameters:
listener
- a listener for request events- Returns:
- this request object
-
onRequestQueued
- Parameters:
listener
- a listener for request queued event- Returns:
- this request object
-
onRequestBegin
- Parameters:
listener
- a listener for request begin event- Returns:
- this request object
-
onRequestHeaders
- Parameters:
listener
- a listener for request headers event- Returns:
- this request object
-
onRequestCommit
- Parameters:
listener
- a listener for request commit event- Returns:
- this request object
-
onRequestContent
- Parameters:
listener
- a listener for request content events- Returns:
- this request object
-
onRequestSuccess
- Parameters:
listener
- a listener for request success event- Returns:
- this request object
-
onRequestFailure
- Parameters:
listener
- a listener for request failure event- Returns:
- this request object
-
onResponseBegin
- Parameters:
listener
- a listener for response begin event- Returns:
- this request object
-
onResponseHeader
- Parameters:
listener
- a listener for response header event- Returns:
- this request object
-
onResponseHeaders
Registers a listener for the headers event.
Note that the response headers at this event may be different from the headers received in
onResponseHeader(Response.HeaderListener)
as specified inResponse.getHeaders()
.- Parameters:
listener
- a listener for response headers event- Returns:
- this request object
-
onResponseContent
- Parameters:
listener
- a consuming listener for response content events- Returns:
- this request object
-
onResponseContentAsync
- Parameters:
listener
- an asynchronous listener for response content events- Returns:
- this request object
-
onResponseContentSource
- Parameters:
listener
- a listener for drivingContent.Source
- Returns:
- this request object
-
onResponseSuccess
- Parameters:
listener
- a listener for response success event- Returns:
- this request object
-
onResponseFailure
- Parameters:
listener
- a listener for response failure event- Returns:
- this request object
-
onPush
Sets a handler for pushed resources.
When resources are pushed from the server, the given
pushHandler
is invoked for every pushed resource. The parameters to theBiFunction
are this request and the synthesized request for the pushed resource. TheBiFunction
should return aCompleteListener
that may also implement other listener interfaces to be notified of various response events, ornull
to signal that the pushed resource should be canceled.- Parameters:
pushHandler
- a handler for pushed resource events- Returns:
- this request object
-
onComplete
- Parameters:
listener
- a listener for complete event- Returns:
- this request object
-
send
Sends this request and returns the response.This method should be used when a simple blocking semantic is needed, and when it is known that the response content can be buffered without exceeding memory constraints.
For example, this method is not appropriate to download big files from a server; consider using
send(Response.CompleteListener)
instead, passing your ownResponse.Listener
or a utility listener such asInputStreamResponseListener
.The method returns when the
complete event
is fired.- Returns:
- a
ContentResponse
for this request - Throws:
InterruptedException
- if send thread is interruptedTimeoutException
- if send times outExecutionException
- if execution fails- See Also:
-
send
Sends this request and asynchronously notifies the given listener for response events.
This method should be used when the application needs to be notified of the various response events as they happen, or when the application needs to efficiently manage the response content.
The listener passed to this method may implement not only
Response.CompleteListener
but also other response listener interfaces, and all the events implemented will be notified. This allows application code to write a single listener class to handle all relevant events.- Parameters:
listener
- the listener that receives response events
-
abort
Attempts to abort the send of this request.- Parameters:
cause
- the abort cause, must not be null- Returns:
- whether the abort succeeded
-
getAbortCause
Throwable getAbortCause()- Returns:
- the abort cause passed to
abort(Throwable)
, or null if this request has not been aborted
-