Package com.gmt2001.httpclient
Class HttpClientResponse
java.lang.Object
com.gmt2001.httpclient.HttpClientResponse
The request parameters and response data from a
HttpClient
request- Author:
- gmt2001
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
HttpClientResponse
(Exception exception, boolean isSuccess, HttpMethod method, String requestBody, byte[] responseBody, HttpHeaders requestHeaders, HttpHeaders responseHeaders, HttpResponseStatus responseCode, URI url) Constructorprotected
HttpClientResponse
(Exception exception, String requestBody, byte[] responseBody, URI url, HttpClientResponse response) Constructor -
Method Summary
Modifier and TypeMethodDescriptionReturns any exception that may have been thrown during the requestboolean
Indicates if there was an exception not related to JSON decodingboolean
hasJson()
Indicates if the response was JSON, noJSONException
was thrown during parsing, and no other exceptions were thrown during the requestboolean
Indicates if an exception was thrown during JSON parsingboolean
Indicates if the response was not JSON or failed JSON parsingboolean
Indicates if the request is an overall success (no exceptions and status code < 400)json()
Returns the response body as a JSONObject, if it was a valid stringified JSON objectReturns the exception raised if JSON parsing of the response body failedReturns the response body as aJSONObject
, if it was a valid stringified JSON objectmethod()
Returns the HTTP request methodbyte[]
Returns the response bodyReturns the request body, if the method was POST/PUT/PATCHReturns the request headersReturns the actual URL that was returned, after following any redirectsReturns the response body as a stringReturns the HTTP response status codeReturns the response headersurl()
Returns the URL requested
-
Constructor Details
-
HttpClientResponse
protected HttpClientResponse(Exception exception, String requestBody, byte[] responseBody, URI url, HttpClientResponse response) Constructor- Parameters:
exception
- any exception that may have been raisedrequestBody
- the request body for POST/PUT/PATCHresponseBody
- the response bodyurl
- the URL requestedresponse
- the response metadata object
-
HttpClientResponse
protected HttpClientResponse(Exception exception, boolean isSuccess, HttpMethod method, String requestBody, byte[] responseBody, HttpHeaders requestHeaders, HttpHeaders responseHeaders, HttpResponseStatus responseCode, URI url) Constructor- Parameters:
exception
- any exception that may have been raisedisSuccess
- whether the request is considered a complete successmethod
- the HTTP method used for the requestrequestBody
- the request body for POST/PUT/PATCHresponseBody
- the response bodyrequestHeaders
- the headers sent with the requestresponseHeaders
- the headers returned in the responseresponseCode
- the response status codeurl
- the URL requested
-
-
Method Details
-
exception
Returns any exception that may have been thrown during the request- Returns:
- the
Exception
if one was thrown;null
if there was none
-
hasException
public boolean hasException()Indicates if there was an exception not related to JSON decoding- Returns:
true
if there is an exception, which can be retrieved usingexception()
-
isSuccess
public boolean isSuccess()Indicates if the request is an overall success (no exceptions and status code < 400)- Returns:
true
ifhasException()
isfalse
andresponseCode()
is < 400
-
json
Returns the response body as a JSONObject, if it was a valid stringified JSON object- Returns:
- the
JSONObject
returned by the response, if the response was JSON
-
jsonOrThrow
Returns the response body as aJSONObject
, if it was a valid stringified JSON objectIf the response body looked like JSON but failed decoding, throws a
JSONException
If a
JSONException
was not caught but another exception was, it is thrownIf the response was a success but the body did not start with
{
, throwsNotJSONException
- Returns:
- the
JSONObject
if a valid JSON object was decoded from the response body - Throws:
JSONException
- if the response body appears to be JSON, but failed to be decoded into aJSONObject
Exception
- if any other exception ocurred during the request, it is thrownNotJSONException
- if the response was a success but the body did not contain valid JSON
-
hasJson
public boolean hasJson()Indicates if the response was JSON, noJSONException
was thrown during parsing, and no other exceptions were thrown during the request- Returns:
true
if a validJSONObject
can be successfully retrieved fromjson()
orjsonOrThrow()
-
isNotJson
public boolean isNotJson()Indicates if the response was not JSON or failed JSON parsingtrue
could indicate any state that would makehasJson()
returnfalse
, such as the response not being JSON, throwing aJSONException
during parsing, or another exception being thrown during the request- Returns:
- the negated value of
hasJson()
-
jsonException
Returns the exception raised if JSON parsing of the response body failedThis is normally a
JSONException
, but could rarely be some other exception- Returns:
- the exception
-
hasJsonException
public boolean hasJsonException()Indicates if an exception was thrown during JSON parsing- Returns:
true
if an exception is available fromjsonException()
-
method
Returns the HTTP request method- Returns:
- the request method
-
requestBody
Returns the request body, if the method was POST/PUT/PATCH- Returns:
- the request body;
null
if there was no body
-
responseBody
Returns the response body as a string- Returns:
- the response body as a UTF-8 encoded string
-
rawResponseBody
public byte[] rawResponseBody()Returns the response body- Returns:
- the raw bytes of the response body
-
requestHeaders
Returns the request headers- Returns:
- the request headers
-
responseHeaders
Returns the response headers- Returns:
- the reponse headers
-
responseCode
Returns the HTTP response status code- Returns:
- the status code
-
url
Returns the URL requested- Returns:
- the
URI
requested
-
resourceUrl
Returns the actual URL that was returned, after following any redirects- Returns:
- the real
URI
that was returned after redirects were followed
-