httpClient

@Stability.Volatile
val httpClient: CouchbaseHttpClient

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

import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.http.CouchbaseHttpClient
import com.couchbase.client.kotlin.http.HttpBody
import com.couchbase.client.kotlin.http.HttpTarget
import com.couchbase.client.kotlin.http.NameValuePairs
import com.couchbase.client.kotlin.http.formatPath
import kotlinx.coroutines.runBlocking
fun main() { 
   //sampleStart 
   // 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() }
} 
   //sampleEnd
}
import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.http.CouchbaseHttpClient
import com.couchbase.client.kotlin.http.HttpBody
import com.couchbase.client.kotlin.http.HttpTarget
import com.couchbase.client.kotlin.http.NameValuePairs
import com.couchbase.client.kotlin.http.formatPath
import kotlinx.coroutines.runBlocking
fun main() { 
   //sampleStart 
   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)
} 
   //sampleEnd
}
import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.http.CouchbaseHttpClient
import com.couchbase.client.kotlin.http.HttpBody
import com.couchbase.client.kotlin.http.HttpTarget
import com.couchbase.client.kotlin.http.NameValuePairs
import com.couchbase.client.kotlin.http.formatPath
import kotlinx.coroutines.runBlocking
fun main() { 
   //sampleStart 
   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)
} 
   //sampleEnd
}
import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.http.CouchbaseHttpClient
import com.couchbase.client.kotlin.http.HttpBody
import com.couchbase.client.kotlin.http.HttpTarget
import com.couchbase.client.kotlin.http.NameValuePairs
import com.couchbase.client.kotlin.http.formatPath
import kotlinx.coroutines.runBlocking
fun main() { 
   //sampleStart 
   runBlocking {
    val response = httpClient.post(
        target = HttpTarget.manager(), // port 8091 (or equivalent)
        path = formatPath("/some/arbitrary/path"),
        body = HttpBody.json("{\"foo\":\"bar\"}")
    )
    println(response)
} 
   //sampleEnd
}