Package kong.unirest.core
Class ByteResponse
- java.lang.Object
-
- kong.unirest.core.ByteResponse
-
- All Implemented Interfaces:
HttpResponse<byte[]>
public class ByteResponse extends Object
-
-
Constructor Summary
Constructors Constructor Description ByteResponse(RawResponse r, ProgressMonitor downloadMonitor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]
getBody()
static byte[]
getBytes(InputStream is)
Cookies
getCookies()
return a cookie collection parse from the set-cookie headerHeaders
getHeaders()
Optional<UnirestParsingException>
getParsingError()
If the transformation to the body failed by an exception it will be kept hereprotected String
getRawBody()
HttpRequestSummary
getRequestSummary()
int
getStatus()
String
getStatusText()
<E> HttpResponse<T>
ifFailure(Class<? extends E> errorClass, Consumer<HttpResponse<E>> consumer)
If the response was NOT a 200-series response or a mapping exception happened.HttpResponse<T>
ifFailure(Consumer<HttpResponse<T>> consumer)
If the response was NOT a 200-series response or a mapping exception happened.HttpResponse<T>
ifSuccess(Consumer<HttpResponse<T>> consumer)
If the response was a 200-series response.static boolean
isGzipped(String value)
boolean
isSuccess()
<V> HttpResponse<V>
map(Function<T,V> func)
Map the Response into another response with a different body<V> V
mapBody(Function<T,V> func)
Map the body into another type<E> E
mapError(Class<? extends E> errorClass)
Map the body into a error class if the response was NOT a 200-series response or a mapping exception happened.protected void
setParsingException(String originalBody, RuntimeException e)
-
-
-
Constructor Detail
-
ByteResponse
public ByteResponse(RawResponse r, ProgressMonitor downloadMonitor)
-
-
Method Detail
-
getBytes
public static byte[] getBytes(InputStream is) throws IOException
- Throws:
IOException
-
isGzipped
public static boolean isGzipped(String value)
-
getBody
public byte[] getBody()
- Specified by:
getBody
in interfaceHttpResponse<byte[]>
- Returns:
- the body
-
getRawBody
protected String getRawBody()
-
getStatus
public int getStatus()
- Specified by:
getStatus
in interfaceHttpResponse<T>
- Returns:
- the HTTP status code.
-
getStatusText
public String getStatusText()
- Specified by:
getStatusText
in interfaceHttpResponse<T>
- Returns:
- status text
-
getHeaders
public Headers getHeaders()
- Specified by:
getHeaders
in interfaceHttpResponse<T>
- Returns:
- Response Headers (map) with same case as server response.
For instance use
getHeaders().getFirst("Location")
and notgetHeaders().getFirst("location")
to get first header "Location"
-
getParsingError
public Optional<UnirestParsingException> getParsingError()
Description copied from interface:HttpResponse
If the transformation to the body failed by an exception it will be kept here- Specified by:
getParsingError
in interfaceHttpResponse<T>
- Returns:
- a possible RuntimeException. Checked exceptions are wrapped in a UnirestException
-
mapBody
public <V> V mapBody(Function<T,V> func)
Description copied from interface:HttpResponse
Map the body into another type- Specified by:
mapBody
in interfaceHttpResponse<T>
- Type Parameters:
V
- The return type of the function- Parameters:
func
- a function to transform a body type to something else.- Returns:
- the return type
-
map
public <V> HttpResponse<V> map(Function<T,V> func)
Description copied from interface:HttpResponse
Map the Response into another response with a different body- Specified by:
map
in interfaceHttpResponse<T>
- Type Parameters:
V
- The return type of the function- Parameters:
func
- a function to transform a body type to something else.- Returns:
- the return type
-
setParsingException
protected void setParsingException(String originalBody, RuntimeException e)
-
isSuccess
public boolean isSuccess()
- Specified by:
isSuccess
in interfaceHttpResponse<T>
- Returns:
- true if the response was a 200-series response and no mapping exception happened, else false
-
ifSuccess
public HttpResponse<T> ifSuccess(Consumer<HttpResponse<T>> consumer)
Description copied from interface:HttpResponse
If the response was a 200-series response. Invoke this consumer can be chained with ifFailure- Specified by:
ifSuccess
in interfaceHttpResponse<T>
- Parameters:
consumer
- a function to consume a HttpResponse- Returns:
- the same response
-
ifFailure
public HttpResponse<T> ifFailure(Consumer<HttpResponse<T>> consumer)
Description copied from interface:HttpResponse
If the response was NOT a 200-series response or a mapping exception happened. Invoke this consumer can be chained with ifSuccess- Specified by:
ifFailure
in interfaceHttpResponse<T>
- Parameters:
consumer
- a function to consume a HttpResponse- Returns:
- the same response
-
mapError
public <E> E mapError(Class<? extends E> errorClass)
Description copied from interface:HttpResponse
Map the body into a error class if the response was NOT a 200-series response or a mapping exception happened. Uses the system Object Mapper- Specified by:
mapError
in interfaceHttpResponse<T>
- Type Parameters:
E
- the response type- Parameters:
errorClass
- the class for the error- Returns:
- the error object
-
ifFailure
public <E> HttpResponse<T> ifFailure(Class<? extends E> errorClass, Consumer<HttpResponse<E>> consumer)
Description copied from interface:HttpResponse
If the response was NOT a 200-series response or a mapping exception happened. map the original body into a error type and invoke this consumer can be chained with ifSuccess- Specified by:
ifFailure
in interfaceHttpResponse<T>
- Type Parameters:
E
- the type of error class to map the body- Parameters:
errorClass
- the class of the error type to map toconsumer
- a function to consume a HttpResponse- Returns:
- the same response
-
getCookies
public Cookies getCookies()
Description copied from interface:HttpResponse
return a cookie collection parse from the set-cookie header- Specified by:
getCookies
in interfaceHttpResponse<T>
- Returns:
- a Cookies collection
-
getRequestSummary
public HttpRequestSummary getRequestSummary()
- Specified by:
getRequestSummary
in interfaceHttpResponse<T>
- Returns:
- a Summary of the HttpRequest that created this response
-
-