Class CouchbaseHttpClient

java.lang.Object
com.couchbase.client.java.http.CouchbaseHttpClient

public class CouchbaseHttpClient extends Object
A specialized HTTP client for making requests to Couchbase Server.

Get an instance by calling Cluster.httpClient().

Example usage:

 public static String getAutoFailoverSettings(Cluster cluster) {
   HttpResponse response = cluster.httpClient().get(
       HttpTarget.manager(),
       HttpPath.of("/settings/autoFailover"));

   if (!response.success()) {
     throw new RuntimeException(
         "Failed to get auto-failover settings. HTTP status code " +
             response.statusCode() + "; " + response.contentAsString());
   }

   return response.contentAsString();
 }

 public static void disableAutoFailover(Cluster cluster) {
   HttpResponse response = cluster.httpClient().post(
       HttpTarget.manager(),
       HttpPath.of("/settings/autoFailover"),
       HttpPostOptions.httpPostOptions()
           .body(HttpBody.form(Map.of("enabled", "false"))));

   if (!response.success()) {
     throw new RuntimeException(
         "Failed to disable auto-failover. HTTP status code " +
             response.statusCode() + "; " + response.contentAsString());
   }
 }
 
See Also:
  • Constructor Details

  • Method Details

    • get

      public HttpResponse get(HttpTarget target, HttpPath path)
      Issues a GET request with default options (no query parameters).

      To specify query parameters, use the overload that takes HttpGetOptions or include the query string in the path.

    • get

      public HttpResponse get(HttpTarget target, HttpPath path, HttpGetOptions options)
      Issues a GET request with the given options.

      Specify query parameters via the options:

       httpClient.get(target, path, HttpGetOptions.httpGetOptions()
           .queryString(Map.of("foo", "bar")));
       
    • post

      public HttpResponse post(HttpTarget target, HttpPath path)
      Issues a POST request with no body and default options.

      To specify a request body, use the overload that takes HttpPostOptions.

    • post

      public HttpResponse post(HttpTarget target, HttpPath path, HttpPostOptions options)
      Issues a POST request with the given options.

      Specify a request body via the options:

       // form data
       httpClient.post(target, path, HttpPostOptions.httpPostOptions()
           .body(HttpBody.form(Map.of("foo", "bar")));
      
       // JSON document
       httpClient.post(target, path, HttpPostOptions.httpPostOptions()
           .body(HttpBody.json("{}")));
       
    • put

      public HttpResponse put(HttpTarget target, HttpPath path)
      Issues a PUT request with no body and default options.

      To specify a request body, use the overload that takes HttpPutOptions.

    • put

      public HttpResponse put(HttpTarget target, HttpPath path, HttpPutOptions options)
      Issues a PUT request with the given options.

      Specify a request body via the options:

       // form data
       httpClient.put(target, path, HttpPostOptions.httpPutOptions()
           .body(HttpBody.form(Map.of("foo", "bar")));
      
       // JSON document
       httpClient.put(target, path, HttpPostOptions.httpPutOptions()
           .body(HttpBody.json("{}")));
       
    • delete

      public HttpResponse delete(HttpTarget target, HttpPath path)
      Issues a DELETE request with default options.
    • delete

      public HttpResponse delete(HttpTarget target, HttpPath path, HttpDeleteOptions options)
      Issues a DELETE request with the given options.