Class HttpResponse
- All Implemented Interfaces:
Closeable,AutoCloseable
Represents an incoming HTTP response.
This class encapsulates an HTTP response, including the HTTP status code, headers, and body. It provides methods to get these properties.
This class is useful when you want to process an HTTP response received from a server. For example, you can use it to get the status code to check if the request was successful, get the headers to check for any additional information, and get the body to process the content of the response.
Note: This class implements Closeable, so you should call the close() method when you're done
with the HTTP response to free any resources associated with it.
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedHttpResponse(HttpRequest request) Creates an instance ofHttpResponse. -
Method Summary
Modifier and TypeMethodDescriptionbuffer()Gets a newresponseobject wrapping this response with its content buffered into memory.voidclose()Closes the response content stream, if any.abstract Flux<ByteBuffer> getBody()Get the publisher emitting response content chunks.Gets theBinaryDatathat represents the body of the response.abstract Mono<byte[]> Gets the response content as abyte[].Gets the response content as anInputStream.Gets the response content as aString.getBodyAsString(Charset charset) Gets the response content as aString.abstract HttpHeadersGet all response headers.getHeaderValue(HttpHeaderName headerName) Lookup a response header with the providerHttpHeaderName.abstract StringgetHeaderValue(String name) Deprecated.final HttpRequestGets therequestwhich resulted in this response.abstract intGet the response status code.voidwriteBodyTo(WritableByteChannel channel) Transfers body bytes to theWritableByteChannel.writeBodyToAsync(AsynchronousByteChannel channel) Transfers body bytes to theAsynchronousByteChannel.
-
Constructor Details
-
HttpResponse
Creates an instance ofHttpResponse.- Parameters:
request- TheHttpRequestthat resulted in thisHttpResponse.
-
-
Method Details
-
getStatusCode
public abstract int getStatusCode()Get the response status code.- Returns:
- The response status code
-
getHeaderValue
Deprecated.UsegetHeaderValue(HttpHeaderName)as it provides better performance.Lookup a response header with the provided name.- Parameters:
name- the name of the header to lookup.- Returns:
- the value of the header, or null if the header doesn't exist in the response.
-
getHeaderValue
Lookup a response header with the providerHttpHeaderName.- Parameters:
headerName- the name of the header to lookup.- Returns:
- the value of the header, or null if the header doesn't exist in the response.
-
getHeaders
Get all response headers.- Returns:
- the response headers
-
getBody
Get the publisher emitting response content chunks.Returns a stream of the response's body content. Emissions may occur on Reactor threads which should not be blocked. Blocking should be avoided as much as possible/practical in reactive programming but if you do use methods like
block()on the stream then be sure to usepublishOnbefore the blocking call.- Returns:
- The response's content as a stream of
ByteBuffer.
-
getBodyAsBinaryData
Gets theBinaryDatathat represents the body of the response.Subclasses should override this method.
- Returns:
- The
BinaryDataresponse body.
-
getBodyAsByteArray
Gets the response content as abyte[].- Returns:
- The response content as a
byte[].
-
getBodyAsString
Gets the response content as aString.By default, this method will inspect the response body for containing a byte order mark (BOM) to determine the encoding of the string (UTF-8, UTF-16, etc.). If a BOM isn't found this will default to using UTF-8 as the encoding, if a specific encoding is required use
getBodyAsString(Charset).- Returns:
- The response content as a
String.
-
getBodyAsString
Gets the response content as aString. -
getBodyAsInputStream
Gets the response content as anInputStream.- Returns:
- The response content as an
InputStream.
-
getRequest
Gets therequestwhich resulted in this response.- Returns:
- The
requestwhich resulted in this response.
-
buffer
Gets a newresponseobject wrapping this response with its content buffered into memory.- Returns:
- A new
responsewith the content buffered.
-
writeBodyToAsync
Transfers body bytes to theAsynchronousByteChannel.- Parameters:
channel- The destinationAsynchronousByteChannel.- Returns:
- A
Monothat completes when transfer is completed. - Throws:
NullPointerException- Whenchannelis null.
-
writeBodyTo
Transfers body bytes to theWritableByteChannel.- Parameters:
channel- The destinationWritableByteChannel.- Throws:
IOException- When I/O operation fails.NullPointerException- Whenchannelis null.
-
close
public void close()Closes the response content stream, if any.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
getHeaderValue(HttpHeaderName)as it provides better performance.