DefaultRequest
Sets default request parameters. Used to add common headers and URL for a request. Note that trailing slash in base URL and leading slash in request URL are important. The rules to calculate a final URL:
Request URL doesn't start with slash
Base URL ends with slash -> concat strings. Example: base =
https://example.com/dir/
, request =file.html
, result =https://example.com/dir/file.html
Base URL doesn't end with slash -> remove last path segment of base URL and concat strings. Example: base =
https://example.com/dir/deafult_file.html
, request =file.html
, result =https://example.com/dir/file.html
Request URL starts with slash -> use request path as is. Example: base =
https://example.com/dir/deafult_file.html
, request =/root/file.html
, result =https://example.com/root/file.html
Headers of the builder will be pre-populated with request headers. You can use HeadersBuilder.contains, HeadersBuilder.appendIfNameAbsent and HeadersBuilder.appendIfNameAndValueAbsent to avoid appending some header twice.
Usage:
val client = HttpClient {
defaultRequest {
url("https://base.url/dir/")
headers.appendIfNameAbsent(HttpHeaders.ContentType, ContentType.Application.Json)
}
}
client.get("file")
// <- requests "https://base.url/dir/file", ContentType = Application.Json
client.get("/other_root/file")
// <- requests "https://base.url/other_root/file", ContentType = Application.Json
client.get("//other.host/path")
// <- requests "https://other.host/path", ContentType = Application.Json
client.get("https://some.url") { HttpHeaders.ContentType = ContentType.Application.Xml }
// <- requests "https://some.url/", ContentType = Application.Xml
Types
Configuration object for DefaultRequestBuilder plugin