Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB On-the-fly attachments through command-line

PROBLEM

I want to be able to attach one/multiple attachment(s) as the document is created, through the command-line (see below). I can only get this to work in Futon (Couchbase), but only after a document has already been created.

I have 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": "[email protected]","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 only results in the following error:

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

Thanks

like image 301
EdvardG Avatar asked Oct 20 '11 10:10

EdvardG


People also ask

How do I get all the files in CouchDB?

To insert documents in bulk into a database you need to supply a JSON structure with the array of documents that you want to add to the database. You can either include a document ID, or allow the document ID to be automatically generated.

How do I add data to CouchDB?

Open the Overview page of the database and select New Document option as shown below. When you select the New Document option, CouchDB creates a new database document, assigning it a new id. You can edit the value of the id and can assign your own value in the form of a string.

What is the path for the Web administration tool for CouchDB?

Installing CouchDB After installation, open built-in web interface of CouchDB by visiting the following link: http://127.0.0.1:5984/.

What is cURL in CouchDB?

cURL utility is a way to communicate with CouchDB. It is a tool to transfer data from or to a server, using one of the supported protocols (HTTP, HTTPS, FTP, FTPS, TFTP, DICT, TELNET, LDAP or FILE). The command is designed to work without user interaction.


2 Answers

You must upload your attachment in a separate step, containing the actual attachment file in the request body. So first create your regular document, then issue another request where you upload the file. Here is an example on how to upload 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

like image 77
b_erb Avatar answered Oct 27 '22 12:10

b_erb


This works for me, and seems a little simpler. The first one has to be when creating the doc, if you don't add a rev. My examples uses 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"}
like image 44
JustTrying Avatar answered Oct 27 '22 11:10

JustTrying