Top Description Methods
io.vertx.core.http

public Interface HttpServerResponse

extends WriteStream<Buffer>
Known Direct Implementers
io.vertx.core.http.impl.Http1xServerResponse, io.vertx.core.http.impl.Http2ServerResponse
Annotations
@VertxGen
Imports
io.vertx.codegen.annotations.*, io.vertx.core.AsyncResult, .Future, .Handler, .MultiMap, io.vertx.core.buffer.Buffer, io.vertx.core.net.HostAndPort, io.vertx.core.streams.ReadStream, .WriteStream, java.util.Set

Represents a server-side HTTP response.

An instance of this is created and associated to every instance of HttpServerRequest that.

It allows the developer to control the HTTP response that is sent back to the client for a particular HTTP request.

It contains methods that allow HTTP headers and trailers to be set, and for a body to be written out to the response.

It also allows files to be streamed by the kernel directly from disk to the outgoing HTTP connection, bypassing user space altogether (where supported by the underlying operating system). This is a very efficient way of serving files from the server since buffers do not have to be read one by one from the file and written to the outgoing socket.

It implements io.vertx.core.streams.WriteStream so it can be used with io.vertx.core.streams.Pipe to pipe data with flow control.

Author
Tim Fox

Method Summary

Modifier and TypeMethod and Description
public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
addCookie
(Cookie
the cookie
cookie
)

Add a cookie.

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
bodyEndHandler
(@Nullable Handler<Void>
the handler
handler
)

Provides a handler that will be called after the last part of the body is written to the wire.

public long

Returns:

the total number of bytes written for the body of the response.
bytesWritten
()

public void
close()
Deprecated instead use HttpConnection#close() or reset(long), this method is removed in Vert.x 5

Close the underlying TCP connection corresponding to the request.

public boolean

Returns:

has the underlying TCP connection corresponding to the request already been closed?
closed
()

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
closeHandler
(@Nullable Handler<Void>
the handler
handler
)

Set a close handler for the response, this is called when the underlying connection is closed and the response was still using the connection.

public HttpServerResponse
public Future<Void>

Returns:

a future completed with the body result
end
(String
the string to write before ending the response
chunk
)

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

Same as end(Buffer) but writes a String in UTF-8 encoding before ending the response.

public void
end(String chunk, Handler<AsyncResult<Void>> handler)

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

Same as end(String) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the body result
end
(String
the string to write before ending the response
chunk
,
String
the encoding to use
enc
)

Same as end(Buffer) but writes a String with the specified encoding before ending the response.

public void
end(String chunk, String enc, Handler<AsyncResult<Void>> handler)

Same as end(String, String) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the body result
end
(Buffer
the buffer to write before ending the response
chunk
)

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Same as end() but writes some data to the response body before ending.

public void
end(Buffer chunk, Handler<AsyncResult<Void>> handler)

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Same as end(Buffer) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the body result
end
()

Redeclares as abstract: io.vertx.core.streams.WriteStream.end.

Ends the response.

public boolean

Returns:

has the response already ended?
ended
()

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
endHandler
(@Nullable Handler<Void>
the handler
handler
)

Set an end handler for the response.

public HttpServerResponse
public int

Returns:

the HTTP status code of the response. The default is 200 representing OK.
getStatusCode
()

public String

Returns:

the HTTP status message of the response. If this is not specified a default value will be used depending on what setStatusCode has been set to.
getStatusMessage
()

public MultiMap

Returns:

The HTTP headers
headers
()

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
headersEndHandler
(@Nullable Handler<Void>
the handler
handler
)

Provide a handler that will be called just before the headers are written to the wire.

public boolean

Returns:

have the headers for the response already been written?
headWritten
()

public boolean

Returns:

is the response chunked?
isChunked
()

public default HttpServerResponse
public default Future<HttpServerResponse>
push(HttpMethod method, String host, String path)
Deprecated instead use push(HttpMethod, HostAndPort, String)

Same as push(HttpMethod, String, String, Handler) but with an handler called when the operation completes

public default HttpServerResponse
public default Future<HttpServerResponse>
push(HttpMethod method, HostAndPort host, String path)

Same as push(HttpMethod, String, String, Handler) but with an handler called when the operation completes

public default HttpServerResponse
push(HttpMethod method, String path, MultiMap headers, Handler<AsyncResult<HttpServerResponse>> handler)

Like push(HttpMethod, String, String, MultiMap, Handler) with the host copied from the current request.

public default Future<HttpServerResponse>
push(HttpMethod method, String path, MultiMap headers)

Same as push(HttpMethod, String, MultiMap, Handler) but with an handler called when the operation completes

public default HttpServerResponse
push(HttpMethod method, String path, Handler<AsyncResult<HttpServerResponse>> handler)

Like push(HttpMethod, String, String, MultiMap, Handler) with the host copied from the current request.

public default Future<HttpServerResponse>
push(HttpMethod method, String path)

Same as push(HttpMethod, String, Handler) but with an handler called when the operation completes

public default HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
push
(HttpMethod
the method of the promised request
method
,
String
the authority of the promised request
host
,
String
the path of the promised request
path
,
MultiMap
the headers of the promised request
headers
,
Handler<AsyncResult<HttpServerResponse>>
the handler notified when the response can be written
handler
)
Deprecated

Push a response to the client.

public Future<HttpServerResponse>

Returns:

a future notified when the response can be written
push
(HttpMethod
the method of the promised request
method
,
String
the host of the promised request
host
,
String
the path of the promised request
path
,
MultiMap
the headers of the promised request
headers
)

Push a response to the client.

public default HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
push
(HttpMethod
the method of the promised request
method
,
HostAndPort
the authority of the promised request
authority
,
String
the path of the promised request
path
,
MultiMap
the headers of the promised request
headers
,
Handler<AsyncResult<HttpServerResponse>>
the handler notified when the response can be written
handler
)

Push a response to the client.

public Future<HttpServerResponse>
push(HttpMethod method, HostAndPort authority, String path, MultiMap headers)

Same as push(HttpMethod, String, String, MultiMap, Handler) but with an handler called when the operation completes

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
putHeader
(String
the header name
name
,
String
the header value.
value
)

Put an HTTP header

public HttpServerResponse
putHeader(CharSequence name, CharSequence value)

Like putHeader(String, String) but using CharSequence

public HttpServerResponse
putHeader(String name, Iterable<String> values)

Like putHeader(String, String) but providing multiple values via a String Iterable

public HttpServerResponse
putHeader(CharSequence name, Iterable<CharSequence> values)

Like putHeader(String, Iterable) but with CharSequence Iterable

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
putTrailer
(String
the trailer name
name
,
String
the trailer value
value
)

Put an HTTP trailer

public HttpServerResponse
putTrailer(CharSequence name, CharSequence value)

Like putTrailer(String, String) but using CharSequence

public HttpServerResponse
putTrailer(String name, Iterable<String> values)

Like putTrailer(String, String) but providing multiple values via a String Iterable

public HttpServerResponse
putTrailer(CharSequence name, Iterable<CharSequence> value)

Like putTrailer(String, Iterable) but with CharSequence Iterable

public default @Nullable Cookie

Returns:

the cookie, if it existed, or null
removeCookie
(String
the name of the cookie
name
)

Expire a cookie, notifying a User Agent to remove it from its cookie jar.

public @Nullable Cookie

Returns:

the cookie, if it existed, or null
removeCookie
(String
the name of the cookie
name
,
boolean invalidate)

Remove a cookie from the cookie set.

public default @Nullable Cookie

Returns:

the cookie, if it existed, or null
removeCookie
(String
the name of the cookie
name
,
String
the domain of the cookie
domain
,
String
the path of the cookie
path
)

Expires a cookie from the cookie set.

public @Nullable Cookie

Returns:

the cookie, if it existed, or null
removeCookie
(String
the name of the cookie
name
,
String
the domain of the cookie
domain
,
String
the path of the cookie
path
,
boolean invalidate)

Remove a cookie from the cookie set.

public default Set<Cookie>

Returns:

a read only set of affected cookies, if they existed, or an empty set.
removeCookies
(String
the name of the cookie
name
)

Expire all cookies, notifying a User Agent to remove it from its cookie jar.

public Set<Cookie>

Returns:

a read only set of affected cookies, if they existed, or an empty set.
removeCookies
(String
the name of the cookie
name
,
boolean
invalidate from the user agent
invalidate
)

Remove all cookies from the cookie set.

public default boolean
reset()

Reset this HTTP/2 stream with the error code 0.

public boolean

Returns:

true when reset has been performed
reset
(long
the error code
code
)

Reset this response:

  • for HTTP/2, send an HTTP/2 reset frame with the specified error code
  • for HTTP/1.x, close the connection when the current response has not yet been sent
public default void
send(Handler<AsyncResult<Void>>
the completion handler
handler
)

Send the request with an empty body.

public default Future<Void>
send()

Like send(Handler) but returns a Future of the asynchronous result

public default void
send(String body, Handler<AsyncResult<Void>>
the completion handler
handler
)

Send the request with a string body.

public default Future<Void>
send(String body)

Like send(String, Handler) but returns a Future of the asynchronous result

public default void
send(Buffer body, Handler<AsyncResult<Void>>
the completion handler
handler
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Send the request with a buffer body.

public default Future<Void>
send(Buffer body)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Like send(Buffer, Handler) but returns a Future of the asynchronous result

public default void
send(ReadStream<Buffer> body, Handler<AsyncResult<Void>>
the completion handler
handler
)

Send the request with a stream body.

public default Future<Void>
send(ReadStream<Buffer> body)

Like send(ReadStream, Handler) but returns a Future of the asynchronous result

public default Future<Void>

Returns:

a future completed with the body result
sendFile
(String
path to the file to serve
filename
)

Same as sendFile(String, long) using offset @code{0} which means starting from the beginning of the file.

public default Future<Void>

Returns:

a future completed with the body result
sendFile
(String
path to the file to serve
filename
,
long
offset to start serving from
offset
)

Same as sendFile(String, long, long) using length @code{Long.MAX_VALUE} which means until the end of the file.

public Future<Void>

Returns:

a future completed with the body result
sendFile
(String
path to the file to serve
filename
,
long
offset to start serving from
offset
,
long
the number of bytes to send
length
)

Ask the OS to stream a file as specified by filename directly from disk to the outgoing connection, bypassing userspace altogether (where supported by the underlying operating system.

public default HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
sendFile
(String
path to the file to serve
filename
,
Handler<AsyncResult<Void>>
handler that will be called on completion
resultHandler
)

Like sendFile(String) but providing a handler which will be notified once the file has been completely written to the wire.

public default HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
sendFile
(String
path to the file to serve
filename
,
long
the offset to serve from
offset
,
Handler<AsyncResult<Void>>
handler that will be called on completion
resultHandler
)

Like sendFile(String, long) but providing a handler which will be notified once the file has been completely written to the wire.

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
sendFile
(String
path to the file to serve
filename
,
long
the offset to serve from
offset
,
long
length the number of bytes to send
length
,
Handler<AsyncResult<Void>>
handler that will be called on completion
resultHandler
)

Like sendFile(String, long, long) but providing a handler which will be notified once the file has been completely written to the wire.

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
setChunked
(boolean chunked)

If chunked is true, this response will use HTTP chunked encoding, and each call to write to the body will correspond to a new HTTP chunk sent on the wire.

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
setStatusCode
(int statusCode)

Set the status code.

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
setStatusMessage
(String statusMessage)

Set the status message

public default HttpServerResponse
setStreamPriority(StreamPriority
the priority for this request's stream
streamPriority
)

Sets the priority of the associated stream

This is not implemented for HTTP/1.x.

public HttpServerResponse
setWriteQueueMaxSize(int
the max size of the write stream
maxSize
)

Redeclares io.vertx.core.streams.WriteStream.setWriteQueueMaxSize.

Set the maximum size of the write queue to maxSize.

public int

Returns:

the id of the stream of this response, -1 for HTTP/1.x
streamId
()

public MultiMap

Returns:

The HTTP trailers
trailers
()

public Future<Void>

Returns:

a future completed with the body result
write
(String
the string to write
chunk
,
String
the encoding to use
enc
)

Write a String to the response body, encoded using the encoding enc.

public void
write(String chunk, String enc, Handler<AsyncResult<Void>> handler)

Same as write(String, String) but with an handler called when the operation completes

public Future<Void>

Returns:

a future completed with the body result
write
(String
the string to write
chunk
)

Redeclares io.vertx.core.streams.WriteStream.write.

Write a String to the response body, encoded in UTF-8.

public void
write(String chunk, Handler<AsyncResult<Void>> handler)

Redeclares io.vertx.core.streams.WriteStream.write.

Same as write(String) but with an handler called when the operation completes

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
writeContinue
()

Used to write an interim 100 Continue response to signify that the client should send the rest of the request.

public HttpServerResponse

Returns:

a reference to this, so the API can be used fluently
writeCustomFrame
(int
the 8-bit frame type
type
,
int
the 8-bit frame flags
flags
,
Buffer
the frame payload
payload
)
References Deprecated Buffer is deprecated or references (maybe indirectly) at least one deprecated element.

Write an HTTP/2 frame to the response, allowing to extend the HTTP/2 protocol.

public default HttpServerResponse
writeCustomFrame(HttpFrame
the frame to write
frame
)

Like writeCustomFrame(int, int, Buffer) but with an HttpFrame.

public Future<Void>

Returns:

a future
writeEarlyHints
(MultiMap
headers to write
headers
)

Used to write an interim 103 Early Hints response to return some HTTP headers before the final HTTP message.

public void
writeEarlyHints(MultiMap
headers to write
headers
,
Handler<AsyncResult<Void>> handler)

Same as writeEarlyHints(MultiMap) but with an handler called when the operation completes

Inherited from io.vertx.core.streams.WriteStream:
drainHandlerendexceptionHandlerwriteQueueFull