Create bucket using the REST API endpoint

Hello,

I am trying to create a Java application and create a new bucket in Couchbase by calling the REST API endpoint provided.
The endpoint for creating a bucket in Couchbase is: /pools/default/buckets

In my application i am trying to do the same using HttpEntity and rest template…

My code is as follows:

String json= "{\"name\":\"bucketName\", \"ramQuotaMB\":100}";
HttpEntity<String> entity = new HttpEntity<String>(json, headers);
result = restTemplate.postForEntity("http://couchbase-url/pools/default/buckets", entity, String.class);

This gives the error:

org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized

I am not sure as to why is there an authentication problem. I already have a config class where all the credentials have been mentioned. I also tried to modify the couchbase-url by giving the username and password in the url as suggested here: HTTP error 401 Unauthorized getting pools

Thanks!

Hi Jenny,

One possible problem is that the bucket creation endpoint expects the bucket settings to be posted as url-encoded form parameters (content-type application/x-www-form-urlencoded).

The source code for AsyncBucketManager from the Java SDK might be a useful reference. Or you could just use the Couchbase Java SDK’s BucketManager API to create the buckets.

EDIT: Hmmm… that documentation I linked to isn’t very helpful.

Here’s a code snippet for Couchabse SDK 3.x:

Cluster cluster = ...
cluster.buckets().createBucket(
    BucketSettings.create("bucketName")
        .ramQuotaMB(100));

For SDK 2.x, the code is a bit different.

Thanks,
David

Hello David,
Thanks for the response! have tried the Couchbase SDK 3.x and that works. But i was thinking of leveraging the REST Endpoints provided by couchbase.

As per your advise, after changing the content-type to application/x-www-form-urlencoded I was able to create a bucket using the resttemplate as below:

HttpHeaders headers= new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setBasicAuth("username","password");
String param = "name=bucketName&ramQuotaMB=100";
HttpEntity<String> entity = new HttpEntity<String>(param, headers);
result = restTemplate.postForEntity("http://couchbase-url/pools/default/buckets", entity, String.class);

I was looking for documentation where it is specified that bucket creation endpoints expects url-encoded form parameters but couldn’t find anything in the docs. If you know the link, please share.

Anyway, changing the content type and setting the authentication parameters solved the problem. Thanks for the help!

Hi Jenny,

I’m glad you got it working.

I was looking for documentation where it is specified that bucket creation endpoints expects url-encoded form parameters

It’s not explicit, but… the curl examples in Creating and Editing Buckets specify parameters using the -d option. The curl documentation (man curl) describes -d like this:

-d, --data

(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlen- coded. Compare to -F, --form.

a bit further down in that section:

If any of these options is used more than once on the same command line, the data pieces specified will be merged together with a separating &-symbol. Thus, using ‘-d name=daniel -d skill=lousy’ would generate a post chunk that looks like ‘name=daniel&skill=lousy’.

Thanks
David