Package kong.unirest.core
Class BaseRequest<R extends HttpRequest>
java.lang.Object
kong.unirest.core.BaseRequest<R>
- All Implemented Interfaces:
HttpRequest<R>
- Direct Known Subclasses:
HttpRequestBody
,HttpRequestJsonPatch
,HttpRequestMultiPart
,HttpRequestNoBody
,HttpRequestUniBody
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
protected final Config
private Integer
private Instant
private ProgressMonitor
protected Headers
protected HttpMethod
private Optional
<ObjectMapper> private String
protected Path
private HttpClient.Version
-
Constructor Summary
ConstructorsConstructorDescriptionBaseRequest
(BaseRequest httpRequest) BaseRequest
(Config config, HttpMethod method, String url) -
Method Summary
Modifier and TypeMethodDescriptionThe Accept header to send (e.g.HttpResponse
<byte[]> asBytes()
Executes the request and returns the response with the body mapped into a byte[]CompletableFuture
<HttpResponse<byte[]>> Executes the request asynchronously and returns the response with the body mapped into a byte[]CompletableFuture
<HttpResponse<byte[]>> asBytesAsync
(Callback<byte[]> callback) Executes the request asynchronously and returns the response with the body mapped into a byte[]asEmpty()
Executes the request and returns the response without parsing the bodyExecutes the request asynchronously and returns the response without parsing the bodyasEmptyAsync
(Callback<Empty> callback) Executes the request asynchronously and returns a empty response which is passed to a callbackasFile
(String path, CopyOption... copyOptions) Executes the request and writes the contents into a fileasFileAsync
(String path, CopyOption... copyOptions) asynchronously executes the request and writes the contents into a fileasFileAsync
(String path, Callback<File> callback, CopyOption... copyOptions) asynchronously executes the request and writes the contents into a fileasJson()
Executes the request and returns the response with the body mapped into a JsonNodeExecutes the request asynchronously and returns the response with the body mapped into a JsonNodeasJsonAsync
(Callback<JsonNode> callback) Executes the request asynchronously and returns the response with the body mapped into a JsonNode<T> HttpResponse
<T> Executes the request and returns the response with the body mapped into T by a configured ObjectMapper<T> HttpResponse
<T> asObject
(Function<RawResponse, T> function) Execute the request and pass the raw response to a function for mapping.<T> HttpResponse
<T> asObject
(GenericType<T> genericType) Executes the request and returns the response with the body mapped into T by a configured ObjectMapper<T> CompletableFuture
<HttpResponse<T>> asObjectAsync
(Class<? extends T> responseClass) Executes the request asynchronously and returns response with the body mapped into T by a configured ObjectMapper<T> CompletableFuture
<HttpResponse<T>> asObjectAsync
(Class<? extends T> responseClass, Callback<T> callback) Executes the request asynchronously, mapping to a type via the configured object mapper and then passed to a callback handler.<T> CompletableFuture
<HttpResponse<T>> asObjectAsync
(Function<RawResponse, T> function) Executes the request asynchronously, and pass the raw response to a function for mapping.<T> CompletableFuture
<HttpResponse<T>> asObjectAsync
(GenericType<T> genericType) Executes the request asynchronously, and use a GenericType with the ObjectMapper<T> CompletableFuture
<HttpResponse<T>> asObjectAsync
(GenericType<T> genericType, Callback<T> callback) Executes the request asynchronously, and use a GenericType with the ObjectMapper<T> PagedList
<T> asPaged
(Function<HttpRequest, HttpResponse> mappingFunction, Function<HttpResponse<T>, String> linkExtractor) Allows for following paging links common in many APIs.asString()
Executes the request and returns the response with the body mapped into a StringExecutes the request asynchronously and returns the response with the body mapped into a StringasStringAsync
(Callback<String> callback) Executes the request asynchronously and returns the response with the body mapped into a StringBasic auth credentialsAdd a simple cookie headercookie
(Collection<Cookie> cookies) Add a collection of cookie headersAdd a simple cookie headerdownloadMonitor
(ProgressMonitor monitor) sets a download monitor for monitoring the response.boolean
private <T> Function
<RawResponse, HttpResponse<T>> funcResponse
(Function<RawResponse, T> function) private Function
<RawResponse, HttpResponse<Object>> getConsumer
(Consumer<RawResponse> consumer) protected ObjectMapper
(package private) Path
getPath()
getUrl()
gets the version for the request, or null if not set.int
hashCode()
Add a http header, HTTP supports multiple of the same header.headerReplace
(String name, String value) Replace a header value or add it if it doesn't existAdd headers as a mapheadersReplace
(Map<String, String> headerMap) Replace headers as a mapqueryString
(String name, Object value) add a query param to the url.queryString
(String name, Collection<?> value) Add multiple param with the same param name.queryString
(Map<String, Object> parameters) Add query params as a map of name value pairsprivate <E> HttpResponse
<E> request
(Function<RawResponse, HttpResponse<E>> transformer, Class<?> resultType) private <T> CompletableFuture
<HttpResponse<T>> requestAsync
(HttpRequest request, Function<RawResponse, HttpResponse<T>> transformer, CompletableFuture<HttpResponse<T>> callback, Class<?> resultType) requestTimeout
(int millies) Set a timeout for this requestresponseEncoding
(String encoding) The encoding to expect the response to be for cases where the server fails to respond with the proper encodingrouteParam
(String name, String value) add a route param that replaces the matching {name} For example routeParam("name", "fred") will replace {name} in https://localhost/users/{name} to https://localhost/users/fredrouteParam
(Map<String, Object> params) add a route param map that replaces the matching {name} For example routeParam(Map.of("name", "fred")) will replace {name} in https://localhost/users/{name} to https://localhost/users/fredvoid
thenConsume
(Consumer<RawResponse> consumer) Execute the request and pass the raw response to a consumer.void
thenConsumeAsync
(Consumer<RawResponse> consumer) Execute the request asynchronously and pass the raw response to a consumer.private <T> T
version
(HttpClient.Version value) sets the HTTP version for the request.withObjectMapper
(ObjectMapper mapper) Pass a ObjectMapper for the request.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface kong.unirest.core.HttpRequest
accept, getBody
-
Field Details
-
creation
-
callCount
private int callCount -
objectMapper
-
responseEncoding
-
headers
-
config
-
method
-
url
-
connectTimeout
-
downloadMonitor
-
version
-
-
Constructor Details
-
BaseRequest
BaseRequest(BaseRequest httpRequest) -
BaseRequest
BaseRequest(Config config, HttpMethod method, String url)
-
-
Method Details
-
routeParam
Description copied from interface:HttpRequest
add a route param that replaces the matching {name} For example routeParam("name", "fred") will replace {name} in https://localhost/users/{name} to https://localhost/users/fred- Specified by:
routeParam
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
name
- the name of the param (do not include curly braces {}value
- the value to replace the placeholder with- Returns:
- this request builder
-
routeParam
Description copied from interface:HttpRequest
add a route param map that replaces the matching {name} For example routeParam(Map.of("name", "fred")) will replace {name} in https://localhost/users/{name} to https://localhost/users/fred- Specified by:
routeParam
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
params
- a map of path params- Returns:
- this request builder
-
basicAuth
Description copied from interface:HttpRequest
Basic auth credentials- Specified by:
basicAuth
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
username
- the usernamepassword
- the password- Returns:
- this request builder
-
accept
Description copied from interface:HttpRequest
The Accept header to send (e.g. application/json)- Specified by:
accept
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
value
- a valid mime type for the Accept header- Returns:
- this request builder
-
header
Description copied from interface:HttpRequest
Add a http header, HTTP supports multiple of the same header. This will continue to append new values- Specified by:
header
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
name
- name of the headervalue
- value for the header- Returns:
- this request builder
-
headerReplace
Description copied from interface:HttpRequest
Replace a header value or add it if it doesn't exist- Specified by:
headerReplace
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
name
- name of the headervalue
- value for the header- Returns:
- this request builder
-
headers
Description copied from interface:HttpRequest
Add headers as a map- Specified by:
headers
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
headerMap
- a map of headers- Returns:
- this request builder
-
headersReplace
Description copied from interface:HttpRequest
Replace headers as a map- Specified by:
headersReplace
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
headerMap
- a map of headers- Returns:
- this request builder
-
responseEncoding
Description copied from interface:HttpRequest
The encoding to expect the response to be for cases where the server fails to respond with the proper encoding- Specified by:
responseEncoding
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
encoding
- a valid mime type for the Accept header- Returns:
- this request builder
-
cookie
Description copied from interface:HttpRequest
Add a simple cookie header- Specified by:
cookie
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
name
- the name of the cookievalue
- the value of the cookie- Returns:
- this request builder
-
cookie
Description copied from interface:HttpRequest
Add a simple cookie header- Specified by:
cookie
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
cookie
- a cookie- Returns:
- this request builder
-
cookie
Description copied from interface:HttpRequest
Add a collection of cookie headers- Specified by:
cookie
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
cookies
- a cookie- Returns:
- this request builder
-
queryString
Description copied from interface:HttpRequest
Add multiple param with the same param name. queryString("name", Arrays.asList("bob", "linda")) will result in ?name=bob&name=linda- Specified by:
queryString
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
name
- the name of the paramvalue
- a collection of values- Returns:
- this request builder
-
queryString
Description copied from interface:HttpRequest
add a query param to the url. The value will be URL-Encoded- Specified by:
queryString
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
name
- the name of the paramvalue
- the value of the param- Returns:
- this request builder
-
queryString
Description copied from interface:HttpRequest
Add query params as a map of name value pairs- Specified by:
queryString
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
parameters
- a map of params- Returns:
- this request builder
-
requestTimeout
Description copied from interface:HttpRequest
Set a timeout for this request- Specified by:
requestTimeout
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
millies
- the time in millies- Returns:
- this request builder
-
withObjectMapper
Description copied from interface:HttpRequest
Pass a ObjectMapper for the request. This will override any globally configured ObjectMapper- Specified by:
withObjectMapper
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
mapper
- the ObjectMapper- Returns:
- this request builder
-
downloadMonitor
Description copied from interface:HttpRequest
sets a download monitor for monitoring the response. this could be used for drawing a progress bar- Specified by:
downloadMonitor
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
monitor
- a ProgressMonitor- Returns:
- this request builder
-
getDownloadMonitor
-
version
Description copied from interface:HttpRequest
sets the HTTP version for the request. This will override any global config- Specified by:
version
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
value
- the version- Returns:
- this request builder
-
asEmpty
Description copied from interface:HttpRequest
Executes the request and returns the response without parsing the body- Specified by:
asEmpty
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- the basic HttpResponse
-
asEmptyAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns the response without parsing the body- Specified by:
asEmptyAsync
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- a CompletableFuture of a HttpResponse
-
asEmptyAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns a empty response which is passed to a callback- Specified by:
asEmptyAsync
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
callback
- the callback* Executes the request asynchronously and returns the response without parsing the body- Returns:
- a CompletableFuture of a HttpResponse
-
asString
Description copied from interface:HttpRequest
Executes the request and returns the response with the body mapped into a String- Specified by:
asString
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- response
- Throws:
UnirestException
-
asStringAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns the response with the body mapped into a String- Specified by:
asStringAsync
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- a CompletableFuture of a response
-
asStringAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns the response with the body mapped into a String- Specified by:
asStringAsync
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
callback
- a callback handler- Returns:
- a CompletableFuture of a response
-
asBytes
Description copied from interface:HttpRequest
Executes the request and returns the response with the body mapped into a byte[]- Specified by:
asBytes
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- response
-
asBytesAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns the response with the body mapped into a byte[]- Specified by:
asBytesAsync
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- a CompletableFuture of a response
-
asBytesAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns the response with the body mapped into a byte[]- Specified by:
asBytesAsync
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
callback
- a callback handler- Returns:
- a CompletableFuture of a response
-
asJson
Description copied from interface:HttpRequest
Executes the request and returns the response with the body mapped into a JsonNode- Specified by:
asJson
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- response
- Throws:
UnirestException
-
asJsonAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns the response with the body mapped into a JsonNode- Specified by:
asJsonAsync
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- a CompletableFuture of a response
-
asJsonAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns the response with the body mapped into a JsonNode- Specified by:
asJsonAsync
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
callback
- a callback handler- Returns:
- a CompletableFuture of a response
-
asObject
Description copied from interface:HttpRequest
Executes the request and returns the response with the body mapped into T by a configured ObjectMapper- Specified by:
asObject
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the return type- Parameters:
responseClass
- the class to return. This will be passed to the ObjectMapper- Returns:
- a response
- Throws:
UnirestException
-
asObject
Description copied from interface:HttpRequest
Executes the request and returns the response with the body mapped into T by a configured ObjectMapper- Specified by:
asObject
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the return type- Parameters:
genericType
- the genertic type to return. This will be passed to the ObjectMapper- Returns:
- a response
- Throws:
UnirestException
-
asObject
Description copied from interface:HttpRequest
Execute the request and pass the raw response to a function for mapping. This raw response contains the original InputStream and is suitable for reading large responses.- Specified by:
asObject
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- The type of the response mapping- Parameters:
function
- the function to map the response into a object of T- Returns:
- A HttpResponse containing T as the body
-
asObjectAsync
Description copied from interface:HttpRequest
Executes the request asynchronously, and pass the raw response to a function for mapping. This raw response contains the original InputStream and is suitable for reading large responses- Specified by:
asObjectAsync
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the type of the response- Parameters:
function
- a function to map the raw request into a object- Returns:
- a CompletableFuture of a HttpResponse containing the body of T
-
asObjectAsync
Description copied from interface:HttpRequest
Executes the request asynchronously and returns response with the body mapped into T by a configured ObjectMapper- Specified by:
asObjectAsync
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the return type- Parameters:
responseClass
- the class type to map to- Returns:
- a CompletableFuture of a response
-
asObjectAsync
public <T> CompletableFuture<HttpResponse<T>> asObjectAsync(Class<? extends T> responseClass, Callback<T> callback) Description copied from interface:HttpRequest
Executes the request asynchronously, mapping to a type via the configured object mapper and then passed to a callback handler.- Specified by:
asObjectAsync
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the return type- Parameters:
responseClass
- the type for the ObjectMapper to map tocallback
- a callback for handling the body post mapping- Returns:
- a CompletableFuture of a HttpResponse containing the body of T
-
asObjectAsync
Description copied from interface:HttpRequest
Executes the request asynchronously, and use a GenericType with the ObjectMapper- Specified by:
asObjectAsync
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the type of the response- Parameters:
genericType
- the generic type containing the type- Returns:
- a CompletableFuture of a HttpResponse containing the body of T
-
asObjectAsync
public <T> CompletableFuture<HttpResponse<T>> asObjectAsync(GenericType<T> genericType, Callback<T> callback) Description copied from interface:HttpRequest
Executes the request asynchronously, and use a GenericType with the ObjectMapper- Specified by:
asObjectAsync
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the type of the response- Parameters:
genericType
- the generic type containing the typecallback
- a callback for handling the body post mapping- Returns:
- a CompletableFuture of a HttpResponse containing the body of T
-
funcResponse
-
thenConsume
Description copied from interface:HttpRequest
Execute the request and pass the raw response to a consumer. This raw response contains the original InputStream and is suitable for reading large responses- Specified by:
thenConsume
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
consumer
- a consumer function
-
thenConsumeAsync
Description copied from interface:HttpRequest
Execute the request asynchronously and pass the raw response to a consumer. This raw response contains the original InputStream and is suitable for reading large responses- Specified by:
thenConsumeAsync
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
consumer
- a consumer function
-
asFile
Description copied from interface:HttpRequest
Executes the request and writes the contents into a file- Specified by:
asFile
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
path
- The path to the file.copyOptions
- options specifying how the copy should be done- Returns:
- a HttpResponse with the file containing the results
-
asFileAsync
Description copied from interface:HttpRequest
asynchronously executes the request and writes the contents into a file- Specified by:
asFileAsync
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
path
- The path to the file.copyOptions
- options specifying how the copy should be done- Returns:
- a file containing the results
-
asFileAsync
public CompletableFuture<HttpResponse<File>> asFileAsync(String path, Callback<File> callback, CopyOption... copyOptions) Description copied from interface:HttpRequest
asynchronously executes the request and writes the contents into a file- Specified by:
asFileAsync
in interfaceHttpRequest<R extends HttpRequest>
- Parameters:
path
- The path to the file.callback
- a callback for handling the body post mappingcopyOptions
- options specifying how the copy should be done- Returns:
- a file containing the results
-
asPaged
public <T> PagedList<T> asPaged(Function<HttpRequest, HttpResponse> mappingFunction, Function<HttpResponse<T>, String> linkExtractor) Description copied from interface:HttpRequest
Allows for following paging links common in many APIs. Each request will result in the same request (headers, etc) but will use the "next" link provided by the extract function.- Specified by:
asPaged
in interfaceHttpRequest<R extends HttpRequest>
- Type Parameters:
T
- the type of response.- Parameters:
mappingFunction
- a function to return the desired return type leveraging one of the as* methods (asString, asObject, etc).linkExtractor
- a function to extract a "next" link to follow. Retuning a null or empty string ends the paging- Returns:
- a PagedList of your type
-
request
private <E> HttpResponse<E> request(Function<RawResponse, HttpResponse<E>> transformer, Class<?> resultType) -
requestAsync
private <T> CompletableFuture<HttpResponse<T>> requestAsync(HttpRequest request, Function<RawResponse, HttpResponse<T>> transformer, CompletableFuture<HttpResponse<T>> callback, Class<?> resultType) -
getConsumer
-
getHttpMethod
- Specified by:
getHttpMethod
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- The HTTP method of the request
-
getUrl
- Specified by:
getUrl
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- The current URL string for the request
-
getHeaders
- Specified by:
getHeaders
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- the current headers for the request
-
getObjectMapper
-
getRequestTimeout
- Specified by:
getRequestTimeout
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- the connect timeout for this request
-
toSummary
- Specified by:
toSummary
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- a summary for the response, used in metrics
-
getCreationTime
- Specified by:
getCreationTime
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- the instant the request object was created in UTC (not when it was sent).
-
getVersion
Description copied from interface:HttpRequest
gets the version for the request, or null if not set.- Specified by:
getVersion
in interfaceHttpRequest<R extends HttpRequest>
- Returns:
- the version
-
valueOr
-
getPath
Path getPath() -
equals
-
hashCode
public int hashCode()
-