Magazines, Books and Articles

Wednesday, March 18, 2015

A C# library for CouchDB

I was recently working on a .NET project that uses CouchDB v 1.6.1 as its database. As part of this project, I created this library to abstract away the HTTP requests to a CouchDB. It is a work in progress, but the methods available should be sufficient for CRUD operations on a CouchDB database.

The code's written in C# in VS 2013 Community Edition.

You can download the source at https://github.com/nestas-santanu/CouchDBLibrary/releases and use it in your projects that use CouchDB. No strings attached.

Thursday, January 22, 2015

Error using cURL to POST or PUT data formatted in JSON in Windows

If you use cURL in a cmd.exe shell on Windows, an attempt to POST or PUT data formatted in JSON results in an error. An example:

This is the data:
{"Continent":"AS","ContinentName":"Asia","CountryName":"India","Capital":"New Delhi","Iso_Alpha2":"IN","Iso_Numeric":"356","Iso_Alpha3":"IND","FipsCode":"IN"}
curl -i -X PUT -d "{"Continent":"AS","ContinentName":"Asia","CountryName":"India","Capital":"New Delhi","Iso_Alpha2":"IN","Iso_Numeric":"356","Iso_Alpha3":"IND","FipsCode":"IN"}"  -H "Content-type:application/json; charset=UTF-8" http://192.168.1.6:5984/geodb-country-test/IN

HTTP/1.1 400 Bad Request
Server: CouchDB/1.6.1 (Erlang OTP/R16B02)
Date: Thu, 22 Jan 2015 06:28:26 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 48
Cache-Control: must-revalidate

{"error":"bad_request","reason":"invalid_json"}
This is because the cmd.exe shell on Windows has issues parsing the quotes in the data.

One work around is to escape all quotes in the JSON. The following works:
curl -i -X PUT -d "{\"Continent\":\"AS\",\"ContinentName\":\"Asia\",\"CountryName\":\"India\",\"Capital\":\"New Delhi\",\"Iso_Alpha2\":\"IN\",\"Iso_Numeric\":\"356\",\"Iso_Alpha3\":\"IND\",\"FipsCode\":\"IN\"}"  -H "Content-type:application/json; charset=UTF-8" http://192.168.1.6:5984/geodb-country-test/IN

HTTP/1.1 201 Created
Server: CouchDB/1.6.1 (Erlang OTP/R16B02)
Location: http://192.168.1.6:5984/geodb-country-test/IN
ETag: "1-d4c8b330d781b982184e0e6829f434cd"
Date: Thu, 22 Jan 2015 06:31:26 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 65
Cache-Control: must-revalidate

{"ok":true,"id":"IN","rev":"1-d4c8b330d781b982184e0e6829f434cd"}
Escaping all the quotes in the data would be a huge pain; here's an online tool that will do all the donkey's work.

Another work around is to write the data to a file and use it in the following manner:
curl -i -X PUT -d @"C:/Shared Folder/country.json"  -H "Content-type:application/json; charset=UTF-8" http://192.168.1.6:5984/geodb-country-test/countries

HTTP/1.1 201 Created
Server: CouchDB/1.6.1 (Erlang OTP/R16B02)
Location: http://192.168.1.6:5984/geodb-country-test/countries
ETag: "1-568f829b1bcc952ba27ca7a084428390"
Date: Thu, 22 Jan 2015 07:42:12 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 72
Cache-Control: must-revalidate

{"ok":true,"id":"countries","rev":"1-568f829b1bcc952ba27ca7a084428390"}
cURL
Windows Installer