Class HttpClientResponse

java.lang.Object
com.gmt2001.httpclient.HttpClientResponse

public final class HttpClientResponse extends Object
The request parameters and response data from a HttpClient request
Author:
gmt2001
  • Constructor Details

    • HttpClientResponse

      protected HttpClientResponse(Exception exception, String requestBody, byte[] responseBody, URI url, HttpClientResponse response)
      Constructor
      Parameters:
      exception - any exception that may have been raised
      requestBody - the request body for POST/PUT/PATCH
      responseBody - the response body
      url - the URL requested
      response - 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 raised
      isSuccess - whether the request is considered a complete success
      method - the HTTP method used for the request
      requestBody - the request body for POST/PUT/PATCH
      responseBody - the response body
      requestHeaders - the headers sent with the request
      responseHeaders - the headers returned in the response
      responseCode - the response status code
      url - the URL requested
  • Method Details

    • exception

      public Exception 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 using exception()
    • isSuccess

      public boolean isSuccess()
      Indicates if the request is an overall success (no exceptions and status code < 400)
      Returns:
      true if hasException() is false and responseCode() is < 400
    • json

      public JSONObject 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

      public JSONObject jsonOrThrow() throws Exception
      Returns the response body as a JSONObject, if it was a valid stringified JSON object

      If the response body looked like JSON but failed decoding, throws a JSONException

      If a JSONException was not caught but another exception was, it is thrown

      If the response was a success but the body did not start with &#123;, throws NotJSONException

      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 a JSONObject
      Exception - if any other exception ocurred during the request, it is thrown
      NotJSONException - if the response was a success but the body did not contain valid JSON
    • hasJson

      public boolean hasJson()
      Indicates if the response was JSON, no JSONException was thrown during parsing, and no other exceptions were thrown during the request
      Returns:
      true if a valid JSONObject can be successfully retrieved from json() or jsonOrThrow()
    • isNotJson

      public boolean isNotJson()
      Indicates if the response was not JSON or failed JSON parsing

      true could indicate any state that would make hasJson() return false, such as the response not being JSON, throwing a JSONException during parsing, or another exception being thrown during the request

      Returns:
      the negated value of hasJson()
    • jsonException

      public Exception jsonException()
      Returns the exception raised if JSON parsing of the response body failed

      This 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 from jsonException()
    • method

      public HttpMethod method()
      Returns the HTTP request method
      Returns:
      the request method
    • requestBody

      public String requestBody()
      Returns the request body, if the method was POST/PUT/PATCH
      Returns:
      the request body; null if there was no body
    • responseBody

      public String 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

      public HttpHeaders requestHeaders()
      Returns the request headers
      Returns:
      the request headers
    • responseHeaders

      public HttpHeaders responseHeaders()
      Returns the response headers
      Returns:
      the reponse headers
    • responseCode

      public HttpResponseStatus responseCode()
      Returns the HTTP response status code
      Returns:
      the status code
    • url

      public URI url()
      Returns the URL requested
      Returns:
      the URI requested
    • resourceUrl

      public URI resourceUrl()
      Returns the actual URL that was returned, after following any redirects
      Returns:
      the real URI that was returned after redirects were followed