HttpResponse Class
- java.
lang. Object - com.
azure. core. http. HttpResponse
- com.
Implements
public abstract class HttpResponse
implements Closeable
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.
Constructor Summary
Modifier | Constructor | Description |
---|---|---|
protected | HttpResponse(HttpRequest request) |
Creates an instance of HttpResponse. |
Method Summary
Modifier and Type | Method and Description |
---|---|
Http |
buffer()
Gets a new HttpResponse object wrapping this response with its content buffered into memory. |
void |
close()
Closes the response content stream, if any. |
abstract
Flux<Byte |
getBody()
Get the publisher emitting response content chunks. |
Binary |
getBodyAsBinaryData()
Gets the BinaryData that represents the body of the response. |
abstract Mono<byte[]> |
getBodyAsByteArray()
Gets the response content as a |
Mono<Input |
getBodyAsInputStream()
Gets the response content as an InputStream. |
abstract Mono<String> |
getBodyAsString()
Gets the response content as a String. |
abstract Mono<String> |
getBodyAsString(Charset charset)
Gets the response content as a String. |
String |
getHeaderValue(HttpHeaderName headerName)
Lookup a response header with the provider HttpHeaderName. |
abstract String |
getHeaderValue(String name)
Deprecated
Use getHeaderValue(HttpHeaderName headerName) as it provides better performance.
Lookup a response header with the provided name. |
abstract
Http |
getHeaders()
Get all response headers. |
final
Http |
getRequest()
Gets the HttpRequest which resulted in this response. |
abstract int |
getStatusCode()
Get the response status code. |
void |
writeBodyTo(WritableByteChannel channel)
Transfers body bytes to the WritableByteChannel. |
Mono<Void> |
writeBodyToAsync(AsynchronousByteChannel channel)
Transfers body bytes to the AsynchronousByteChannel. |
Methods inherited from java.lang.Object
Constructor Details
HttpResponse
Method Details
buffer
public HttpResponse buffer()
Gets a new HttpResponse object wrapping this response with its content buffered into memory.
Returns:
close
public void close()
Closes the response content stream, if any.
getBody
public abstract Flux
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 use publishOn
before the blocking call.
Returns:
getBodyAsBinaryData
public BinaryData getBodyAsBinaryData()
Gets the BinaryData that represents the body of the response.
Subclasses should override this method.
Returns:
getBodyAsByteArray
public abstract Mono
Gets the response content as a byte[]
.
Returns:
byte[]
.getBodyAsInputStream
public Mono
Gets the response content as an InputStream.
Returns:
getBodyAsString
public abstract Mono
Gets the response content as a String.
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 charset).
Returns:
getBodyAsString
public abstract Mono
Gets the response content as a String.
Parameters:
Returns:
getHeaderValue
public String getHeaderValue(HttpHeaderName headerName)
Lookup a response header with the provider HttpHeaderName.
Parameters:
Returns:
getHeaderValue
@Deprecated
public abstract String getHeaderValue(String name)
Deprecated
Lookup a response header with the provided name.
Parameters:
Returns:
getHeaders
public abstract HttpHeaders getHeaders()
Get all response headers.
Returns:
getRequest
public final HttpRequest getRequest()
Gets the HttpRequest which resulted in this response.
Returns:
getStatusCode
public abstract int getStatusCode()
Get the response status code.
Returns:
writeBodyTo
public void writeBodyTo(WritableByteChannel channel)
Transfers body bytes to the WritableByteChannel.
Parameters:
Throws:
writeBodyToAsync
public Mono
Transfers body bytes to the AsynchronousByteChannel.
Parameters:
Returns:
Applies to
Azure SDK for Java