CouchDB On-the-fly attachments through the command line

PROBLEM

I want to be able to connect one or more attachments (s) as the document is created via the command line (see below). I can get this to work only in Futon (Couchbase), but only after the document has already been created.

I tried the following:

curl -X PUT 'http://username:password@localhost:5984/client_info' curl -X POST 'http://username:password@localhost:5984/client_info' -H 'Content-Type: application/json' -d '{"client_type": "Private", "client_name": "John Doe","client_email": "john@doe.com","client_city": "Toronto","created_at": "2011-09-06 12:45:03","expires_at": "2012-01-01 00:00:00", "_attachments": { "test01.jpg": { "content_type": "image/jpeg", "length": 30189 } } }' 

This results in the following error:

 {"error":"unknown_error","reason":"function_clause"} 

thanks

+8
couchdb couchdb-futon couchbase
source share
3 answers

You must download your application in a separate step containing the actual attachment file in the request body. So first create your regular document, then run another request where you upload the file. The following is an example of loading an attachment using curl (http://guide.couchdb.org/draft/api.html#attachments): curl -v -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=2-2739352689 --data-binary @artwork.jpg -H "Content-Type: image/jpg"

And here is the official API for attachments: http://wiki.apache.org/couchdb/HTTP_Document_API#Standalone_Attachments

+16
source share

This works for me, and it seems a little easier. The first one should be when creating a document, unless you add rev. My examples use the database "test1".

 $ curl -H "Content-Type: image/jpeg" -X PUT --data-binary @test01.jpg 'http://username:password@localhost:5984/test1/client_info/test01.jpg' {"ok":true,"id":"client_info","rev":"1-8584b6af9d0c3347ba08202697f09952"} $ curl -H "Content-Type: image/jpeg" -X PUT --data-binary @test02.jpg 'http://username:password@localhost:5984/test1/client_info/test02.jpg?rev=1-8584b6af9d0c3347ba08202697f09952' {"ok":true,"id":"client_info","rev":"2-623b94aba30944d6744f5c11cf03fc10"} 
+5
source share

Here is a way to load an attachment with the same query as creating the document.

 curl -X POST 'http://user:pass@localhost:5984/client_stuff' -H 'Content-Type: application/json' -d '{"stuff": "stuff", "_attachments": { "empty.gif": { "content_type": "image/gif", "data": "'$(openssl base64 < file.gif)'" } } }' 

Depending on your use case, Base64 encoding might not be that bad.

Additional information: http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments

+2
source share

All Articles