Using the Client
Simple GET request
All HTTP classes offer a fluent interface, too; so you can write:
You can build the request step by step:
Reading Response
When an HTTP request is sent, the whole response is stored in the HttpResponse
instance. You can use the response for various stuff: read the statusCode()
or statusPhrase()
; or any header attribute.
A common thing is how to read the received response body. You may use one of the following methods:
bodyRaw()
- raw body content, always inISO-8859-1
encoding.bodyText()
- body text, i.e. string encoded as specified byContent-Type
header.bodyBytes()
- returns the raw body as a byte array, so e.g. downloaded filecan be saved.
The character encoding used in bodyText()
is one set in the response headers. If the response does not specify the encoding in its headers (but e.g. only on the HTML page), you must specify the encoding with charset()
the method before calling bodyText()
. {: .attn}
Query parameters
Query parameters may be specified in the URL line (but then they have to be encoded correctly):
A better and recommended way is with the query()
method:
You can use query()
for each parameter, or pass many arguments in one call (varargs). You can also provide Map<String, String>
as a parameter too.
Query parameters (as well as headers and form parameters) can be duplicated. Therefore, they are stored in an array internally. Use method removeQuery
to remove some parameters or overloaded methods to replace a parameter.
Finally, you can reach the internal query map, that actually holds all parameters:
Authentication
Basic authentication is made easy:
Token-based authentication:
POST and form parameters
Use form()
in the same way as query()
to specify form parameters. Everything that is said for query()
applies to the form()
.
Upload files
Again, it's easy: just add the file form parameter. Here is one real-world example:
Monitor upload progress
When uploading a large file, it is helpful to monitor the progress. For that purpose, you can use HttpProgressListener
like this:
Before the upload starts, HttpProgressListener
calculates the callbackSize
- the size of the chunk in bytes that will be transferred. By default, this size equals 1%
of the total size. Moreover, it is never less than 512
bytes.
HttpProgressListener
contains the inner field size
with the total size of the request. Note that this is the size of the whole request, not only the files! This is the actual number of bytes that are going to be sent, and it is always a bit larger than file size (due to protocol overhead).
Headers
Add or reach header parameters with the method header()
. Some common header parameters are already defined as methods, so you will find contentType()
etc.
There are some shortcut methods that are commonly used:
contentTypeJson()
- specifies JSON content typeacceptJson()
- accepts JSON content.
GZipped content
Just unzip()
the response.
The unzip()
method is safe; it will not fail if the response is not zipped.
Set the body
You can set the request body manually:
Setting the body discards all previously set form()
parameters.
Charsets and Encodings
By default, query and form parameters are encoded in UTF-8.
You can set form encoding similarly. Moreover, form posting detects the value of charset in the "Content-Type" header, and if present, it will be used.
With received content, body()
method always returns the raw string (encoded as ISO-8859-1). To get the string in usable form, use the method bodyText()
. This method uses a provided charset from the "Content-Type" header and encodes the body string.
Following redirection
By default HttpRequest
does not follow redirection response. This can be changed by setting the followRedirects(true)
. Now redirect responses are followed. When redirection is enabled, the original URL will NOT be preserved in the request object!
Asynchronous sending
When send()
is called, the program blocks until the response is received. By using sendAsync()
the execution of the sending is passed to Javas fork-join pool, and will be executed asynchronously. Method returns CompletableFuture<Response>
.
Last updated