[Haskell-cafe] Haskell + CouchDB, anyone?

Clint Moore cmoore at wamboli.com
Fri May 28 15:17:31 EDT 2010


Hi there,

I'm trying to use any version of couchdb with Haskell and
Database.CouchDB and I'm not having a ton of luck.  I've talked to
others in the past that have used it without a problem so I'm
wondering what it is that I'm doing wrong.

I can connect, and create new databases without a problem - do any of
you couchdb-using haskell people see a problem with this line?

runCouchDB' $ newDoc (db "testify") $ JSObject $ toJSObject
[("name",JSString $ toJSString $ "value")]

I figured I would start small and just try to put something basic into
a document.

The rest of this email is also at a gist at http://gist.github.com/417442

Here is a list of the revisions of couchdb and the different errors
I'm getting back as well as some of the code I'm using to try to test
this..  Hopefully it will help.


{-

CouchDB 0.11.0:
[error] [<0.95.0>] attempted upload of invalid JSON undefined
[info] [<0.95.0>] 127.0.0.1 - - 'PUT' /honks/cmoore? 400

CouchDB Trunk as of 5/28 - 10:26 PST:
[info] [<0.95.0>] 127.0.0.1 - - 'PUT' /honks? 201
[debug] [<0.114.0>] 'PUT' /honks/cmoore? {1,1}
Headers: [{'Connection',"keep-alive"},
         {'Content-Type',"application/json"},
         {'Host',"localhost:5984"},
         {'User-Agent',"hs-HTTP-4000.0.5"}]
[debug] [<0.114.0>] OAuth Params: []
[error] [<0.114.0>] attempted upload of invalid JSON undefined
[info] [<0.114.0>] 127.0.0.1 - - 'PUT' /honks/cmoore? 400
[debug] [<0.114.0>] httpd 400 error response:
 {"error":"bad_request","reason":"invalid UTF-8 JSON"}

CouchDB rev 818083:
[debug] [<0.107.0>] 'PUT' /honks/cmoore? {1,1}
Headers: [{'Connection',"keep-alive"},
         {'Content-Type',"application/json"},
         {'Host',"localhost:5984"},
         {'User-Agent',"hs-HTTP-4000.0.5"}]
[debug] [<0.107.0>] OAuth Params: []
[debug] [<0.107.0>] Minor error in HTTP request: {invalid_json,undefined}
[debug] [<0.107.0>] Stacktrace: [{mochijson2,decode,1},
            {couch_httpd_db,db_doc_req,3},
            {couch_httpd_db,do_db_req,2},
            {couch_httpd,handle_request,5},
            {mochiweb_http,headers,5},
            {proc_lib,init_p_do_apply,3}]
[info] [<0.107.0>] 127.0.0.1 - - 'PUT' /honks/cmoore? 500
[debug] [<0.107.0>] httpd 500 error response:
 {"error":"invalid_json","reason":"undefined"}

CouchDB 0.10.1:
[error] [<0.278.0>] attempted upload of invalid JSON undefined
[info] [<0.278.0>] 127.0.0.1 - - 'POST' /testify? 400

-}

module Main where

import Text.JSON
import Database.CouchDB
import Database.CouchDB.JSON

data Person = Person {
     name :: String,
     email :: String
} deriving (Eq,Show)

instance JSON Person where
   showJSON (Person nm em) = JSObject $ toJSObject [("name", showJSON
nm),("email", showJSON em)]
   readJSON val = do
     obj <- jsonObject val
     nm <- jsonField "name" obj
     em <- jsonField "email" obj
     return (Person nm em)


create = runCouchDB "localhost" 5984 $ createDB "honks"

main :: IO ()
main = do
 _ <- runCouchDB "localhost" 5984 $ newNamedDoc (db "honks") (doc
"cmoore") $ Person "cmoore" "cmoore at w.com"
 putStrLn "Honk."

---

Later, against 0.10.1:

cmoore at cathedral:~/tmp$ ghci
GHCi, version 6.12.2: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :m +Database.CouchDB
Prelude Database.CouchDB> :m +Text.JSON
Prelude Database.CouchDB Text.JSON> :set prompt "> "
> runCouchDB' $ createDB "testify"
Loading package array-0.3.0.0 ... linking ... done.
Loading package syb-0.1.0.2 ... linking ... done.
Loading package base-3.0.3.2 ... linking ... done.
Loading package bytestring-0.9.1.6 ... linking ... done.
Loading package mtl-1.1.0.2 ... linking ... done.
Loading package parsec-2.1.0.1 ... linking ... done.
Loading package network-2.2.1.5 ... linking ... done.
Loading package old-locale-1.0.0.2 ... linking ... done.
Loading package old-time-1.0.0.4 ... linking ... done.
Loading package HTTP-4000.0.8 ... linking ... done.
Loading package containers-0.3.0.0 ... linking ... done.
Loading package pretty-1.0.1.1 ... linking ... done.
Loading package json-0.4.4 ... linking ... done.
Loading package CouchDB-0.10.2 ... linking ... done.
> runCouchDB' $ newDoc (db "testify") $ JSObject $ toJSObject [("name",JSString $ toJSString $ "value")]
*** Exception: HTTP/1.1 400 Bad Request
Server: CouchDB/0.10.1 (Erlang OTP/R13B)
Date: Fri, 28 May 2010 18:46:36 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 54
Cache-Control: must-revalidate


More information about the Haskell-Cafe mailing list