Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB - Create a document with an attachment in one step?

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!

like image 840
golmschenk Avatar asked Mar 25 '23 01:03

golmschenk


1 Answers

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"
like image 97
Kxepal Avatar answered Apr 02 '23 00:04

Kxepal