Package com.gmt2001.httpclient
Class HttpClient
java.lang.Object
com.gmt2001.httpclient.HttpClient
Performs HTTP requests
- Author:
- gmt2001
-
Method Summary
Modifier and TypeMethodDescriptionstatic HttpHeaders
Creates a new, emptyHttpHeaders
static HttpHeaders
createHeaders
(boolean isMutatorWithBody, boolean isJson) Creates a newHttpHeaders
with some default headers filled instatic HttpHeaders
createHeaders
(HttpMethod method, boolean isJson) Creates a newHttpHeaders
with some default headers filled instatic String
createQuery
(Map<String, String> query) Takes a Map of query params and converts it to a query stringstatic HttpClientResponse
Shortcut to perform aHttpMethod.DELETE
request with the default headersstatic HttpClientResponse
delete
(URI url, HttpHeaders requestHeaders) Shortcut to perform aHttpMethod.DELETE
request with the specified headersstatic HttpClientResponse
Shortcut to perform aHttpMethod.GET
request with the default headersstatic HttpClientResponse
get
(URI url, HttpHeaders requestHeaders) Shortcut to perform aHttpMethod.GET
request with the specified headersstatic HttpClientResponse
Shortcut to perform aHttpMethod.HEAD
request with the default headersstatic HttpClientResponse
head
(URI url, HttpHeaders requestHeaders) Shortcut to perform a HEAD with the specified headersstatic boolean
isMutatorWithBody
(HttpMethod method) Indicates if the provided method is a mutator that can provide a request bodystatic HttpClientResponse
patch
(URI url, HttpHeaders requestHeaders, String requestBody) Shortcut to perform aHttpMethod.PATCH
request with the specified headersstatic HttpClientResponse
Shortcut to URL-encode a map of patch data then submit it as aHttpMethod.PATCH
request with the specified headersstatic HttpClientResponse
patch
(URI url, HttpHeaders requestHeaders, JSONObject json) Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PATCH
request with the specified headersstatic HttpClientResponse
Shortcut to perform a form-urlencodedHttpMethod.PATCH
request with the default headersstatic HttpClientResponse
Shortcut to URL-encode a map of put data then submit it as a form-urlencodedHttpMethod.PATCH
request with the default headersstatic HttpClientResponse
patch
(URI url, JSONObject json) Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PATCH
request with the default headersstatic HttpClientResponse
post
(URI url, HttpHeaders requestHeaders, String requestBody) Shortcut to perform aHttpMethod.POST
request with the specified headersstatic HttpClientResponse
Shortcut to URL-encode a map of post data then submit it as aHttpMethod.POST
request with the specified headersstatic HttpClientResponse
post
(URI url, HttpHeaders requestHeaders, JSONObject json) Shortcut to stringify aJSONObject
and submit it as aHttpMethod.POST
request with the specified headersstatic HttpClientResponse
Shortcut to perform a form-urlencodedHttpMethod.POST
request with the default headersstatic HttpClientResponse
Shortcut to URL-encode a map of post data then submit it as a form-urlencodedHttpMethod.POST
request with the default headersstatic HttpClientResponse
post
(URI url, JSONObject json) Shortcut to stringify aJSONObject
and submit it as aHttpMethod.POST
request with the default headersstatic HttpClientResponse
put
(URI url, HttpHeaders requestHeaders, String requestBody) Shortcut to perform aHttpMethod.PUT
request with the specified headersstatic HttpClientResponse
Shortcut to URL-encode a map of put data then submit it as aHttpMethod.PUT
request with the specified headersstatic HttpClientResponse
put
(URI url, HttpHeaders requestHeaders, JSONObject json) Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PUT
request with the specified headersstatic HttpClientResponse
Shortcut to perform a form-urlencodedHttpMethod.PUT
request with the default headersstatic HttpClientResponse
Shortcut to URL-encode a map of put data then submit it as a form-urlencodedHttpMethod.PUT
request with the default headersstatic HttpClientResponse
put
(URI url, JSONObject json) Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PUT
request with the default headersstatic HttpClientResponse
request
(HttpMethod method, URI url, HttpHeaders requestHeaders, String requestBody) Performs an HTTP requeststatic 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
-
Method Details
-
request
public static HttpClientResponse request(HttpMethod method, URI url, HttpHeaders requestHeaders, String requestBody) Performs an HTTP request- Parameters:
method
- the HTTP methodurl
- the URL to requestrequestHeaders
- the request headers to sendrequestBody
- the request body to send if method isHttpMethod.POST
,HttpMethod.PUT
, orHttpMethod.PATCH
;null
for other methods- Returns:
- a
HttpClientResponse
with the results
-
head
Shortcut to perform aHttpMethod.HEAD
request with the default headers- Parameters:
url
- the URL to request- Returns:
- a
HttpClientResponse
with the results
-
head
Shortcut to perform a HEAD with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to send- Returns:
- a
HttpClientResponse
with the results
-
get
Shortcut to perform aHttpMethod.GET
request with the default headers- Parameters:
url
- the URL to request- Returns:
- a
HttpClientResponse
with the results
-
get
Shortcut to perform aHttpMethod.GET
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to send- Returns:
- a
HttpClientResponse
with the results
-
post
Shortcut to perform a form-urlencodedHttpMethod.POST
request with the default headers- Parameters:
url
- the URL to requestrequestBody
- the request body- Returns:
- a
HttpClientResponse
with the results
-
post
Shortcut to perform aHttpMethod.POST
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to sendrequestBody
- the request body- Returns:
- a
HttpClientResponse
with the results
-
post
Shortcut to URL-encode a map of post data then submit it as a form-urlencodedHttpMethod.POST
request with the default headers- Parameters:
url
- the URL to requestpostData
- 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 aHttpMethod.POST
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to sendpostData
- a map of post data- Returns:
- a
HttpClientResponse
with the results
-
post
Shortcut to stringify aJSONObject
and submit it as aHttpMethod.POST
request with the default headers- Parameters:
url
- the URL to sendjson
- the JSON object to send- Returns:
- a
HttpClientResponse
with the results
-
post
Shortcut to stringify aJSONObject
and submit it as aHttpMethod.POST
request with the specified headers- Parameters:
url
- the URL to sendrequestHeaders
- the headers to sendjson
- the JSON object to send- Returns:
- a
HttpClientResponse
with the results
-
put
Shortcut to perform a form-urlencodedHttpMethod.PUT
request with the default headers- Parameters:
url
- the URL to sendrequestBody
- the request body- Returns:
- a
HttpClientResponse
with the results
-
put
Shortcut to perform aHttpMethod.PUT
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to sendrequestBody
- the request body- Returns:
- a
HttpClientResponse
with the results
-
put
Shortcut to URL-encode a map of put data then submit it as a form-urlencodedHttpMethod.PUT
request with the default headers- Parameters:
url
- the URL to requestputData
- 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 aHttpMethod.PUT
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to sendputData
- a map of put data- Returns:
- a
HttpClientResponse
with the results
-
put
Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PUT
request with the default headers- Parameters:
url
- the URL to sendjson
- the JSON object to send- Returns:
- a
HttpClientResponse
with the results
-
put
Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PUT
request with the specified headers- Parameters:
url
- the URL to sendrequestHeaders
- the headers to sendjson
- the JSON object to send- Returns:
- a
HttpClientResponse
with the results
-
patch
Shortcut to perform a form-urlencodedHttpMethod.PATCH
request with the default headers- Parameters:
url
- the URL to sendrequestBody
- the request body- Returns:
- a
HttpClientResponse
with the results
-
patch
Shortcut to perform aHttpMethod.PATCH
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to sendrequestBody
- the request body- Returns:
- a
HttpClientResponse
with the results
-
patch
Shortcut to URL-encode a map of put data then submit it as a form-urlencodedHttpMethod.PATCH
request with the default headers- Parameters:
url
- the URL to requestpatchData
- 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 aHttpMethod.PATCH
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to sendpatchData
- a map of patch data- Returns:
- a
HttpClientResponse
with the results
-
patch
Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PATCH
request with the default headers- Parameters:
url
- the URL to sendjson
- the JSON object to send- Returns:
- a
HttpClientResponse
with the results
-
patch
Shortcut to stringify aJSONObject
and submit it as aHttpMethod.PATCH
request with the specified headers- Parameters:
url
- the URL to sendrequestHeaders
- the headers to sendjson
- the JSON object to send- Returns:
- a
HttpClientResponse
with the results
-
delete
Shortcut to perform aHttpMethod.DELETE
request with the default headers- Parameters:
url
- the URL to request- Returns:
- a
HttpClientResponse
with the results
-
delete
Shortcut to perform aHttpMethod.DELETE
request with the specified headers- Parameters:
url
- the URL to requestrequestHeaders
- the headers to send- Returns:
- a
HttpClientResponse
with the results
-
urlencodePost
Converts a map of post data into a URL-encoded string suitable for the content type application/x-www-form-urlencodedTo put only a name without the
=value
, set the value tonull
- Parameters:
postData
- a map of post data- Returns:
- a URL encoded string
-
createHeaders
Creates a new, emptyHttpHeaders
- Returns:
- an empty
HttpHeaders
-
createHeaders
Creates a newHttpHeaders
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 setisJson
- iftrue
, sets the accept toapplication/json
- Returns:
- a
HttpHeaders
with some default headers, if the parameters require them
-
createHeaders
Creates a newHttpHeaders
with some default headers filled in- Parameters:
isMutatorWithBody
- iftrue
, sets the content type. If isJson isfalse
, sets toapplication/x-www-form-urlencoded
isJson
- iftrue
, sets the accept toapplication/json
. If isMutator istrue
, sets the content type as well- Returns:
- a
HttpHeaders
with some default headers, if the parameters require them
-
isMutatorWithBody
Indicates if the provided method is a mutator that can provide a request body- Parameters:
method
- the method to test- Returns:
true
if theHttpMethod
specified is a mutator type
-
createQuery
Takes a Map of query params and converts it to a query stringTo put only a name without the
=value
, set the value tonull
- Parameters:
query
- the query params- Returns:
- a URL encoded string, including the
?
prefix
-