I'm just starting out with CouchDB and I've been able to create a document with an attachment using the two step process:
$ curl -X PUT http://localhost:5984/test/small -H "Content-Type: application/json" -d {}
$ curl -X PUT http://localhost:5984/test/small/attachment?rev=1-967a00dff5e02add41819138abb3284d --data-binary @small.mp4 -H "Content-Type: video/mp4"
I've been searching around for a bit and I've only seen examples where the document is first created and the attachment is subsequently added. Is there a way I can do this in a single command? Thank you much!
You can create/update document and set attachments in same time if you pass their content base64-encoded:
curl -X PUT http://localhost:5984/test/docid -d '{"_attachments": {"message.txt": {"data": "aGVsbG8sIENvdWNoIQ==", "content_type": "text/plain"}}}' -H "Content-Type:application/json"
If document already contains some attachments, don't forget to keep their stubs or they will be removed:
curl -X PUT http://localhost:5984/test/docid -d '{"_rev": "1-c5715e943df193437ca89e66982562a5", "_attachments": {"another_message.txt": {"data": "UmVsYXgh", "content_type": "text/plain"}, "message.txt":{"content_type":"text/plain","revpos":1,"digest":"md5-G8EmVzBQlBaB8+bLeMMzeg==","length":13,"stub":true}}}' -H "Content-Type:application/json"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With