http Client
An HTTP client for the Couchbase REST API.
This is an "escape hatch" to use in areas where the Kotlin SDK's management APIs do not provide full coverage of the REST API.
Samples
// Using the Couchbase HTTP Client to interact with the
// Couchbase REST API.
// Assumes you have Couchbase running locally
// and the travel-sample bucket installed.
val cluster = Cluster.connect("127.0.0.1", "Administrator", "password")
val httpClient = cluster.httpClient
val bucketName = "travel-sample"
try {
runBlocking {
// get bucket stats
val response = httpClient.get(
target = HttpTarget.manager(), // port 8091 (or equivalent)
path = formatPath("/pools/default/buckets/{}/stats", bucketName)
)
with(response) {
check(success) { "Request failed with status code $statusCode and content $contentAsString" }
println(contentAsString)
}
}
} finally {
runBlocking { cluster.disconnect() }
}Content copied to clipboard
runBlocking {
val response = httpClient.get(
target = HttpTarget.manager(), // port 8091 (or equivalent)
path = formatPath("/some/arbitrary/path"),
queryString = NameValuePairs.of(
"color" to "green",
"number" to 37,
"ingredients" to "sugar & spice" // values are automatically url-encoded
)
)
println(response)
}Content copied to clipboard
runBlocking {
val response = httpClient.post(
target = HttpTarget.manager(), // port 8091 (or equivalent)
path = "/some/arbitrary/path",
body = HttpBody.form(
"color" to "green",
"number" to 37,
"ingredients" to "sugar & spice" // values are automatically url-encoded
)
)
println(response)
}Content copied to clipboard
runBlocking {
val response = httpClient.post(
target = HttpTarget.manager(), // port 8091 (or equivalent)
path = formatPath("/some/arbitrary/path"),
body = HttpBody.json("{\"foo\":\"bar\"}")
)
println(response)
}Content copied to clipboard