public interface HttpClient
A new instance of this interface can be configured with it's fluent methods.
These methods return the same HttpClient instance so the configuration can be performed
with method call chain. At the end of this chain the sendAndReceive() method should be called
which executes the call and returns with the response.
The client instance has some default configuration.
method(HttpMethod) method.charset(Charset) method.mimeType(String) method.accept(String) method.Example HTTP API call:
String data = ...;
HttpResponse<String> response = httpClient()
.url("http://myservice.xy/service")
.body(data)
.basicAuthorization("user", "pwd")
.sendAndReceive();
| Modifier and Type | Method and Description |
|---|---|
HttpClient |
accept(String mimeType)
Sets the "Accept" header of the HTTP request.
|
HttpClient |
authorization(String authorization)
Sets the "Authorization" header of the HTTP request.
|
HttpClient |
basicAuthorization(String username,
String password)
Sets the "Authorization" header of the HTTP request.
|
HttpClient |
bearerAuthorization(String token)
Sets the "Authorization" header of the HTTP request.
|
<T> HttpClient |
body(T object)
Sets the body of the HTTP request.
|
HttpClient |
charset(Charset charset)
Sets the charset of the HTTP request's content.
|
HttpClient |
charset(String charsetName)
Sets the charset of the HTTP request's content.
|
HttpClient |
header(String name,
String value)
Adds a HTTP header to the request.
|
HttpClient |
method(HttpMethod method)
Sets the HTTP method of the request.
|
HttpClient |
mimeType(String mimeType)
Sets the MIME type of the HTTP request's content.
|
HttpResponse<String> |
sendAndReceive()
Executes the HTTP request: sends the request and receives the response.
|
HttpClient |
url(String url)
Sets the URL of the HTTP request.
|
HttpClient |
url(URL url)
Sets the URL of the HTTP request.
|
HttpClient |
withoutCertificateValidation()
Turns off SSL certificate validation.
|
HttpClient url(String url)
http://localhost:8080/page,/page or index.html.If the URL is absolute, then it will be used by the HTTP client.
If the URL is relative, then it will be appended to the value of the ibello.url.base configuration property,
and the result URL will be used.
url - relative or absolute URLHttpClient instanceNullPointerException - when the argument is nullHttpClient url(URL url)
url - the URLHttpClient instanceNullPointerException - when the argument is nullHttpClient method(HttpMethod method)
method - the HTTP methodHttpClient instanceHttpClient mimeType(String mimeType)
mimeType - a valid MIME typeHttpClient instanceHttpClient charset(Charset charset)
charset - a java Charset instanceHttpClient instanceHttpClient charset(String charsetName)
charsetName - the name of the charsetHttpClient instance<T> HttpClient body(T object)
File instance, in this case a multipart attachment will be added to the body,InputStream instance, in this case the content of the stream will be the request body,String.T - the type of the body objectobject - the object to be sentHttpClient instanceHttpClient header(String name, String value)
name - the name of the headervalue - the value of the headerHttpClient instanceHttpClient accept(String mimeType)
mimeType - a valid MIME typeHttpClient instanceHttpClient authorization(String authorization)
authorization - the value of the Authorization headerHttpClient instanceHttpClient basicAuthorization(String username, String password)
username - the authorization usernamepassword - the authorization passwordHttpClient instanceHttpClient bearerAuthorization(String token)
token - the authorization tokenHttpClient instanceHttpClient withoutCertificateValidation()
HttpClient instanceHttpResponse<String> sendAndReceive() throws IOException, IllegalArgumentException
IOException - if the communication with the REST server failedIllegalArgumentException - if the response of the REST server was invalidCreated by Kornél Simon, 2022