Class HttpClient

java.lang.Object
com.gmt2001.httpclient.HttpClient

public final class HttpClient extends Object
Performs HTTP requests
Author:
gmt2001
  • Method Details

    • request

      public static HttpClientResponse request(HttpMethod method, URI url, HttpHeaders requestHeaders, String requestBody)
      Performs an HTTP request
      Parameters:
      method - the HTTP method
      url - the URL to request
      requestHeaders - the request headers to send
      requestBody - the request body to send if method is HttpMethod.POST, HttpMethod.PUT, or HttpMethod.PATCH; null for other methods
      Returns:
      a HttpClientResponse with the results
    • postBytes

      public static HttpClientResponse postBytes(URI url, HttpHeaders requestHeaders, byte[] requestBody)
      Performs a HttpMethod.POST request with a binary body, such as an image

      The Content-Length header is always set from the length of the body

      Parameters:
      url - the URL to request
      requestHeaders - the request headers to send, including the Content-Type
      requestBody - the request body to send
      Returns:
      a HttpClientResponse with the results
    • head

      public static HttpClientResponse head(URI url)
      Shortcut to perform a HttpMethod.HEAD request with the default headers
      Parameters:
      url - the URL to request
      Returns:
      a HttpClientResponse with the results
    • head

      public static HttpClientResponse head(URI url, HttpHeaders requestHeaders)
      Shortcut to perform a HEAD with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      Returns:
      a HttpClientResponse with the results
    • get

      public static HttpClientResponse get(URI url)
      Shortcut to perform a HttpMethod.GET request with the default headers
      Parameters:
      url - the URL to request
      Returns:
      a HttpClientResponse with the results
    • get

      public static HttpClientResponse get(URI url, HttpHeaders requestHeaders)
      Shortcut to perform a HttpMethod.GET request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      Returns:
      a HttpClientResponse with the results
    • post

      public static HttpClientResponse post(URI url, String requestBody)
      Shortcut to perform a form-urlencoded HttpMethod.POST request with the default headers
      Parameters:
      url - the URL to request
      requestBody - the request body
      Returns:
      a HttpClientResponse with the results
    • post

      public static HttpClientResponse post(URI url, HttpHeaders requestHeaders, String requestBody)
      Shortcut to perform a HttpMethod.POST request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      requestBody - the request body
      Returns:
      a HttpClientResponse with the results
    • post

      public static HttpClientResponse post(URI url, Map<String,String> postData)
      Shortcut to URL-encode a map of post data then submit it as a form-urlencoded HttpMethod.POST request with the default headers
      Parameters:
      url - the URL to request
      postData - a map of post data
      Returns:
      a HttpClientResponse with the results
    • post

      public static HttpClientResponse post(URI url, HttpHeaders requestHeaders, Map<String,String> postData)
      Shortcut to URL-encode a map of post data then submit it as a HttpMethod.POST request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      postData - a map of post data
      Returns:
      a HttpClientResponse with the results
    • post

      public static HttpClientResponse post(URI url, JSONObject json)
      Shortcut to stringify a JSONObject and submit it as a HttpMethod.POST request with the default headers
      Parameters:
      url - the URL to send
      json - the JSON object to send
      Returns:
      a HttpClientResponse with the results
    • post

      public static HttpClientResponse post(URI url, HttpHeaders requestHeaders, JSONObject json)
      Shortcut to stringify a JSONObject and submit it as a HttpMethod.POST request with the specified headers
      Parameters:
      url - the URL to send
      requestHeaders - the headers to send
      json - the JSON object to send
      Returns:
      a HttpClientResponse with the results
    • put

      public static HttpClientResponse put(URI url, String requestBody)
      Shortcut to perform a form-urlencoded HttpMethod.PUT request with the default headers
      Parameters:
      url - the URL to send
      requestBody - the request body
      Returns:
      a HttpClientResponse with the results
    • put

      public static HttpClientResponse put(URI url, HttpHeaders requestHeaders, String requestBody)
      Shortcut to perform a HttpMethod.PUT request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      requestBody - the request body
      Returns:
      a HttpClientResponse with the results
    • put

      public static HttpClientResponse put(URI url, Map<String,String> putData)
      Shortcut to URL-encode a map of put data then submit it as a form-urlencoded HttpMethod.PUT request with the default headers
      Parameters:
      url - the URL to request
      putData - a map of put data
      Returns:
      a HttpClientResponse with the results
    • put

      public static HttpClientResponse put(URI url, HttpHeaders requestHeaders, Map<String,String> putData)
      Shortcut to URL-encode a map of put data then submit it as a HttpMethod.PUT request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      putData - a map of put data
      Returns:
      a HttpClientResponse with the results
    • put

      public static HttpClientResponse put(URI url, JSONObject json)
      Shortcut to stringify a JSONObject and submit it as a HttpMethod.PUT request with the default headers
      Parameters:
      url - the URL to send
      json - the JSON object to send
      Returns:
      a HttpClientResponse with the results
    • put

      public static HttpClientResponse put(URI url, HttpHeaders requestHeaders, JSONObject json)
      Shortcut to stringify a JSONObject and submit it as a HttpMethod.PUT request with the specified headers
      Parameters:
      url - the URL to send
      requestHeaders - the headers to send
      json - the JSON object to send
      Returns:
      a HttpClientResponse with the results
    • patch

      public static HttpClientResponse patch(URI url, String requestBody)
      Shortcut to perform a form-urlencoded HttpMethod.PATCH request with the default headers
      Parameters:
      url - the URL to send
      requestBody - the request body
      Returns:
      a HttpClientResponse with the results
    • patch

      public static HttpClientResponse patch(URI url, HttpHeaders requestHeaders, String requestBody)
      Shortcut to perform a HttpMethod.PATCH request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      requestBody - the request body
      Returns:
      a HttpClientResponse with the results
    • patch

      public static HttpClientResponse patch(URI url, Map<String,String> patchData)
      Shortcut to URL-encode a map of put data then submit it as a form-urlencoded HttpMethod.PATCH request with the default headers
      Parameters:
      url - the URL to request
      patchData - a map of patch data
      Returns:
      a HttpClientResponse with the results
    • patch

      public static HttpClientResponse patch(URI url, HttpHeaders requestHeaders, Map<String,String> patchData)
      Shortcut to URL-encode a map of patch data then submit it as a HttpMethod.PATCH request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      patchData - a map of patch data
      Returns:
      a HttpClientResponse with the results
    • patch

      public static HttpClientResponse patch(URI url, JSONObject json)
      Shortcut to stringify a JSONObject and submit it as a HttpMethod.PATCH request with the default headers
      Parameters:
      url - the URL to send
      json - the JSON object to send
      Returns:
      a HttpClientResponse with the results
    • patch

      public static HttpClientResponse patch(URI url, HttpHeaders requestHeaders, JSONObject json)
      Shortcut to stringify a JSONObject and submit it as a HttpMethod.PATCH request with the specified headers
      Parameters:
      url - the URL to send
      requestHeaders - the headers to send
      json - the JSON object to send
      Returns:
      a HttpClientResponse with the results
    • delete

      public static HttpClientResponse delete(URI url)
      Shortcut to perform a HttpMethod.DELETE request with the default headers
      Parameters:
      url - the URL to request
      Returns:
      a HttpClientResponse with the results
    • delete

      public static HttpClientResponse delete(URI url, HttpHeaders requestHeaders)
      Shortcut to perform a HttpMethod.DELETE request with the specified headers
      Parameters:
      url - the URL to request
      requestHeaders - the headers to send
      Returns:
      a HttpClientResponse with the results
    • urlencodePost

      public static String urlencodePost(Map<String,String> postData)
      Converts a map of post data into a URL-encoded string suitable for the content type application/x-www-form-urlencoded

      To put only a name without the =value, set the value to null

      Parameters:
      postData - a map of post data
      Returns:
      a URL encoded string
    • createHeaders

      public static HttpHeaders createHeaders()
      Creates a new, empty HttpHeaders
      Returns:
      an empty HttpHeaders
    • createHeaders

      public static HttpHeaders createHeaders(HttpMethod method, boolean isJson)
      Creates a new HttpHeaders with some default headers filled in
      Parameters:
      method - the method that is going to be requested. If it is a mutator method that can provide a body, the content type may be set
      isJson - if true, sets the accept to application/json
      Returns:
      a HttpHeaders with some default headers, if the parameters require them
    • createHeaders

      public static HttpHeaders createHeaders(boolean isMutatorWithBody, boolean isJson)
      Creates a new HttpHeaders with some default headers filled in
      Parameters:
      isMutatorWithBody - if true, sets the content type. If isJson is false, sets to application/x-www-form-urlencoded
      isJson - if true, sets the accept to application/json. If isMutator is true, sets the content type as well
      Returns:
      a HttpHeaders with some default headers, if the parameters require them
    • isMutatorWithBody

      public static boolean isMutatorWithBody(HttpMethod method)
      Indicates if the provided method is a mutator that can provide a request body
      Parameters:
      method - the method to test
      Returns:
      true if the HttpMethod specified is a mutator type
    • createQuery

      public static String createQuery(Map<String,String> query)
      Takes a Map of query params and converts it to a query string

      To put only a name without the =value, set the value to null

      Parameters:
      query - the query params
      Returns:
      a URL encoded string, including the ? prefix