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
ConstructorsModifierConstructorDescriptionprotectedHttpClientResponse(Exception exception, boolean isSuccess, HttpMethod method, String requestBody, byte[] responseBody, HttpHeaders requestHeaders, HttpHeaders responseHeaders, HttpResponseStatus responseCode, URI url) ConstructorprotectedHttpClientResponse(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 requestbooleanIndicates if there was an exception not related to JSON decodingbooleanhasJson()Indicates if the response was JSON, noJSONExceptionwas thrown during parsing, and no other exceptions were thrown during the requestbooleanIndicates if an exception was thrown during JSON parsingbooleanIndicates if the response was not JSON or failed JSON parsingbooleanIndicates 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
Exceptionif one was thrown;nullif there was none
-
hasException
public boolean hasException()Indicates if there was an exception not related to JSON decoding- Returns:
trueif 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:
trueifhasException()isfalseandresponseCode()is < 400
-
json
Returns the response body as a JSONObject, if it was a valid stringified JSON object- Returns:
- the
JSONObjectreturned 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
JSONExceptionIf a
JSONExceptionwas not caught but another exception was, it is thrownIf the response was a success but the body did not start with
{, throwsNotJSONException- Returns:
- the
JSONObjectif 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 aJSONObjectException- 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, noJSONExceptionwas thrown during parsing, and no other exceptions were thrown during the request- Returns:
trueif a validJSONObjectcan be successfully retrieved fromjson()orjsonOrThrow()
-
isNotJson
public boolean isNotJson()Indicates if the response was not JSON or failed JSON parsingtruecould indicate any state that would makehasJson()returnfalse, such as the response not being JSON, throwing aJSONExceptionduring 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:
trueif 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;
nullif 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
URIrequested
-
resourceUrl
Returns the actual URL that was returned, after following any redirects- Returns:
- the real
URIthat was returned after redirects were followed
-