public interface HttpClientRequest extends WriteStream<Buffer>, ReadStream<HttpClientResponse>
Instances are created by an HttpClient
instance, via one of the methods corresponding to the
specific HTTP methods, or the generic request methods. On creation the request will not have been written to the
wire.
Once a request has been obtained, headers can be set on it, and data can be written to its body if required. Once
you are ready to send the request, one of the end()
methods should be called.
Nothing is actually sent until the request has been internally assigned an HTTP connection.
The HttpClient
instance will return an instance of this class immediately, even if there are no HTTP
connections available in the pool. Any requests sent before a connection is assigned will be queued
internally and actually sent when an HTTP connection becomes available from the pool.
The headers of the request are queued for writing either when the end()
method is called, or, when the first
part of the body is written, whichever occurs first.
This class supports both chunked and non-chunked HTTP.
It implements WriteStream
so it can be used with
Pump
to pump data with flow control.
An example of using this class is as follows:
Modifier and Type | Method and Description |
---|---|
HttpConnection |
connection() |
HttpClientRequest |
connectionHandler(Handler<HttpConnection> handler)
Set a connection handler called when an HTTP connection has been established.
|
HttpClientRequest |
continueHandler(Handler<Void> handler)
If you send an HTTP request with the header
Expect set to the value 100-continue
and the server responds with an interim HTTP response with a status code of 100 and a continue handler
has been set using this method, then the handler will be called. |
HttpClientRequest |
drainHandler(Handler<Void> handler)
Set a drain handler on the stream.
|
void |
end()
Ends the request.
|
void |
end(Buffer chunk)
Same as
end() but writes some data to the request body before ending. |
void |
end(String chunk)
Same as
end(Buffer) but writes a String in UTF-8 encoding |
void |
end(String chunk,
String enc)
Same as
end(Buffer) but writes a String with the specified encoding |
HttpClientRequest |
endHandler(Handler<Void> endHandler)
Set an end handler.
|
HttpClientRequest |
exceptionHandler(Handler<Throwable> handler)
Set an exception handler on the write stream.
|
String |
getHost() |
String |
getRawMethod() |
HttpClientRequest |
handler(Handler<HttpClientResponse> handler)
Set a data handler.
|
MultiMap |
headers() |
boolean |
isChunked() |
HttpMethod |
method()
The HTTP method for the request.
|
String |
path() |
HttpClientRequest |
pause()
Pause the
ReadSupport . |
HttpClientRequest |
pushHandler(Handler<HttpClientRequest> handler)
Set a push handler for this request.
The handler is called when the client receives a push promise from the server.
|
HttpClientRequest |
putHeader(CharSequence name,
CharSequence value)
Like
putHeader(String, String) but using CharSequence |
HttpClientRequest |
putHeader(CharSequence name,
Iterable<CharSequence> values)
Like
putHeader(String, Iterable) but using CharSequence |
HttpClientRequest |
putHeader(String name,
Iterable<String> values)
Put an HTTP header with multiple values
|
HttpClientRequest |
putHeader(String name,
String value)
Put an HTTP header
|
String |
query() |
default void |
reset()
Reset this stream with the error code
0 . |
void |
reset(long code)
Reset this stream with the error
code . |
HttpClientRequest |
resume()
Resume reading.
|
HttpClientRequest |
sendHead()
Forces the head of the request to be written before
end() is called on the request or any data is
written to it. |
HttpClientRequest |
sendHead(Handler<HttpVersion> completionHandler)
Like
sendHead() but with an handler after headers have been sent. |
HttpClientRequest |
setChunked(boolean chunked)
If chunked is true then the request will be set into HTTP chunked mode
|
HttpClientRequest |
setHost(String host)
Set the request host.
For HTTP2 it sets the :authority pseudo header otherwise it sets the Host header
|
HttpClientRequest |
setRawMethod(String method)
Set the value the method to send when the method
HttpMethod.OTHER is used. |
HttpClientRequest |
setTimeout(long timeoutMs)
Set's the amount of time after which if the request does not return any data within the timeout period an
TimeoutException will be passed to the exception handler (if provided) and
the request will be closed. |
HttpClientRequest |
setWriteQueueMaxSize(int maxSize)
Set the maximum size of the write queue to
maxSize . |
default int |
streamId() |
String |
uri() |
HttpClientRequest |
write(Buffer data)
Write some data to the stream.
|
HttpClientRequest |
write(String chunk)
Write a
String to the request body, encoded as UTF-8. |
HttpClientRequest |
write(String chunk,
String enc)
Write a
String to the request body, encoded using the encoding enc . |
default HttpClientRequest |
writeCustomFrame(HttpFrame frame)
Like
writeCustomFrame(int, int, Buffer) but with an HttpFrame . |
HttpClientRequest |
writeCustomFrame(int type,
int flags,
Buffer payload)
Write an HTTP/2 frame to the request, allowing to extend the HTTP/2 protocol.
|
writeQueueFull
HttpClientRequest exceptionHandler(Handler<Throwable> handler)
WriteStream
exceptionHandler
in interface ReadStream<HttpClientResponse>
exceptionHandler
in interface StreamBase
exceptionHandler
in interface WriteStream<Buffer>
handler
- the exception handlerHttpClientRequest write(Buffer data)
WriteStream
WriteStream.writeQueueFull()
method before writing. This is done automatically if using a Pump
.write
in interface WriteStream<Buffer>
data
- the data to writeIllegalStateException
- when no response handler is setHttpClientRequest setWriteQueueMaxSize(int maxSize)
WriteStream
maxSize
. You will still be able to write to the stream even
if there is more than maxSize
bytes in the write queue. This is used as an indicator by classes such as
Pump
to provide flow control.setWriteQueueMaxSize
in interface WriteStream<Buffer>
maxSize
- the max size of the write streamHttpClientRequest drainHandler(Handler<Void> handler)
WriteStream
Pump
for an example of this being used.drainHandler
in interface WriteStream<Buffer>
handler
- the handlerHttpClientRequest handler(Handler<HttpClientResponse> handler)
ReadStream
handler
in interface ReadStream<HttpClientResponse>
HttpClientRequest pause()
ReadStream
ReadSupport
. While it's paused, no data will be sent to the dataHandler
pause
in interface ReadStream<HttpClientResponse>
HttpClientRequest resume()
ReadStream
ReadSupport
has been paused, reading will recommence on it.resume
in interface ReadStream<HttpClientResponse>
HttpClientRequest endHandler(Handler<Void> endHandler)
ReadStream
endHandler
in interface ReadStream<HttpClientResponse>
HttpClientRequest setChunked(boolean chunked)
chunked
- true if chunked encodingboolean isChunked()
HttpMethod method()
String getRawMethod()
HttpClientRequest setRawMethod(String method)
HttpMethod.OTHER
is used.method
- the raw methodString uri()
String path()
String query()
HttpClientRequest setHost(String host)
String getHost()
MultiMap headers()
HttpClientRequest putHeader(String name, String value)
name
- The header namevalue
- The header valueHttpClientRequest putHeader(CharSequence name, CharSequence value)
putHeader(String, String)
but using CharSequenceHttpClientRequest putHeader(String name, Iterable<String> values)
name
- The header namevalues
- The header valuesHttpClientRequest putHeader(CharSequence name, Iterable<CharSequence> values)
putHeader(String, Iterable)
but using CharSequenceHttpClientRequest write(String chunk)
String
to the request body, encoded as UTF-8.IllegalStateException
- when no response handler is setHttpClientRequest write(String chunk, String enc)
String
to the request body, encoded using the encoding enc
.IllegalStateException
- when no response handler is setHttpClientRequest continueHandler(Handler<Void> handler)
Expect
set to the value 100-continue
and the server responds with an interim HTTP response with a status code of 100
and a continue handler
has been set using this method, then the handler
will be called.
You can then continue to write data to the request body and later end it. This is normally used in conjunction with
the sendHead()
method to force the request header to be written before the request has ended.
HttpClientRequest sendHead()
end()
is called on the request or any data is
written to it.
This is normally used to implement HTTP 100-continue handling, see continueHandler(io.vertx.core.Handler)
for
more information.
IllegalStateException
- when no response handler is setHttpClientRequest sendHead(Handler<HttpVersion> completionHandler)
sendHead()
but with an handler after headers have been sent. The handler will be called with
the HttpVersion
if it can be determined or null otherwise.void end(String chunk)
end(Buffer)
but writes a String in UTF-8 encodingIllegalStateException
- when no response handler is setvoid end(String chunk, String enc)
end(Buffer)
but writes a String with the specified encodingIllegalStateException
- when no response handler is setvoid end(Buffer chunk)
end()
but writes some data to the request body before ending. If the request is not chunked and
no other data has been written then the Content-Length
header will be automatically setend
in interface WriteStream<Buffer>
IllegalStateException
- when no response handler is setvoid end()
sendHead()
has not been called then
the actual request won't get written until this method gets called.
Once the request has ended, it cannot be used any more,
end
in interface WriteStream<Buffer>
IllegalStateException
- when no response handler is setHttpClientRequest setTimeout(long timeoutMs)
TimeoutException
will be passed to the exception handler (if provided) and
the request will be closed.
Calling this method more than once has the effect of canceling any existing timeout and starting the timeout from scratch.
timeoutMs
- The quantity of time in milliseconds.HttpClientRequest pushHandler(Handler<HttpClientRequest> handler)
HttpClientRequest
, the following methods can be called:
In addition the handler should call the handler(io.vertx.core.Handler<io.vertx.core.http.HttpClientResponse>)
method to set an handler to
process the response.handler
- the handlerdefault void reset()
0
.void reset(long code)
code
.code
- the error codeHttpConnection connection()
HttpConnection
associated with this requestHttpClientRequest connectionHandler(Handler<HttpConnection> handler)
handler
- the handlerHttpClientRequest writeCustomFrame(int type, int flags, Buffer payload)
The frame is sent immediatly and is not subject to flow control.
This method must be called after the request headers have been sent and only for the protocol HTTP/2.
The sendHead(Handler)
should be used for this purpose.
type
- the 8-bit frame typeflags
- the 8-bit frame flagspayload
- the frame payloaddefault int streamId()
default HttpClientRequest writeCustomFrame(HttpFrame frame)
writeCustomFrame(int, int, Buffer)
but with an HttpFrame
.frame
- the frame to writeCopyright © 2016. All rights reserved.