DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. CouchDB HTTP API and Mono

CouchDB HTTP API and Mono

Denzel D. user avatar by
Denzel D.
·
Jul. 20, 10 · Interview
Like (0)
Save
Tweet
Share
7.87K Views

Join the DZone community and get the full member experience.

Join For Free

NOTE: Tested on CouchDB 1.0

I finally got around to work a bit more with CouchDB, that is a document-oriented database – the NoSQL you’ve probably heard about so much lately.

So I decided to experiment with it and since it offered a HTTP API, I decided to try it out with a Mono application on a Ubuntu box (hosted inside a virtual machine). It will surely work as a regular .NET application, so feel free to use the provided samples in Windows as well, but make sure that CouchDB is properly working there first.

Once you install CouchDB, you are able to access it via a web UI called Futon.

Via this interface it is fairly easy to create new databases and content, as well as manage the security settings and the space occupied by the stored data.

The exposed HTTP API allows users to perform several maintenance and management tasks from any application that is able to implement a REST interface.

To test this API, I created a simple Mono console application (C# - using MonoDevelop):

I added the references to System.Net (to work with HTTP requests and responses) and System.IO (to work with streams).

The first method I am going to implement is the one that gets information about all databases that are registered on the server. To be specific, this is the method that will list the existing databases. Here is how it looks like:

static void GetAllDatabases()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:5984/_all_dbs");
request.Method ="GET";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}

If you worked with HTTP requests before, then there is nothing special in the method above. But look at the URL:

http://127.0.0.1:5984/_all_dbs

/_all_dbs is the way to call the function that will display all databases via a GET request. Once you build and run the application, you should see an output similar to this:

["test_db","_users"]

To create a database, a bit of a different approach is used. Here is the method in case:

static void CreateDatabase(string name)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:5984/" + name.ToLower());
request.Method ="PUT";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}

First of all, the request method is PUT instead of GET. Then, as you can see, the URL format passed to the request follows this pattern:

http://127.0.0.1:5984/new_database_name

Notice however, that I am using name.ToLower() – this is because in CouchDB I cannot have databases named with uppercase letters. Also, the only special characters that are allowed are: _, $, (, ), +, -, and /

The database deletion is performed in a similar manner as the CreateDatabase method. All that changes is the method type for the request, which is DELETE.

static void DeleteDatabase(string name)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:5984/" + name.ToLower());
request.Method ="DELETE";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}

Both for CreateDatabase and DeleteDatabase, the output for a successful operation should look like this:

{"ok":true}

Getting database information is also very similar, but it is using a GET request with the URL of the same pattern (http://127.0.0.1:5984/database_name).

static void GetDatabaseInfo(string name)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:5984/" + name.ToLower());
request.Method ="GET";

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}
}
}

The output for the call should look like this (JSON-formatted):

{
"db_name":"test_db",
"doc_count":0,
"doc_del_count":0,
"update_seq":0,
"purge_seq":0,
"compact_running":false,
"disk_size":79,
"instance_start_time":"1279485090324739",
"disk_format_version":5
}

Although the API might seem a bit limited, it offers the basic functionality set that enables the developers to access the data on a CouchDB server from anywhere via a simple REST implementation.

Hopefully in the next CouchDB releases we will see a more extended API that will allow developers to manage the server and the data on it.

For more information on the CouchDB HTTP API, read here.

API Database Mono (software) application Requests Document-oriented database Data (computing) Interface (computing) Web Protocols

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevOps for Developers: Continuous Integration, GitHub Actions, and Sonar Cloud
  • Create Spider Chart With ReactJS
  • How To Choose the Right Streaming Database
  • Custom Validators in Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: