DefaultRequest

class 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:

  1. 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

  2. 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.appendIfNameMissing and HeadersBuilder.appendIfNameAndValueMissing to avoid appending some header twice.

Usage:

val client = HttpClient {
defaultRequest {
url("https://base.url/dir/")
headers.appendIfNameMissing(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

Link copied to clipboard
class DefaultRequestBuilder : HttpMessageBuilder

Configuration object for DefaultRequestBuilder plugin

Link copied to clipboard