CouchDB Attaching Files

You can attached file in couchDB. If you want to attach a file to the document then all you need to do is send a PUT request to the server. The file contains :-
1. Name
2. MIME type
3. Number of bytes

Syntax

$ curl -vX PUT http://127.0.0.1:5984/database-name/database-id
/filename?rev=document rev_id --data-binary @filename -H "Content-Type:
type of the content"


-H:-Used to mention the content type of the file
--data-binary@:-Tells cURL to read a file’s contents into the HTTP request body.


Example :

We have to attach a file named LFC.jpg, to the document with id johndoe in the database named contacts by sending PUT request to CouchDB.

First you have to get the revision ID of the document. If you don't know _rev, you can find yourself in the document using the command below. Now after getting the revision ID, using the _rev value, send the PUT request to the CouchDB server as shown below.

Find revision ID
$ curl -X GET http://127.0.0.1:5984/contacts/johndoe
{
"_id" : "johndoe",
"_rev" : "2-04d8eac1680d237ca25b68b36b8899d3 "
}
-----------------------------------------------------
-----------------------------------------------------
Attaching Files
$ curl -vX PUT http://127.0.0.1:5984/contacts/johndoe/LFC.jpg?rev=1-
967a00dff5e02add41819138abb3284d --data-binary @LFC.jpg -H "ContentType:
image/jpg"



Verification :

If you want to verify whether the document is attached or not, you can verify by fetching the contents of the document.

$ curl -X GET http://127.0.0.1:5984/contacts/johndoe
{
"_id": "johndoe",
"_rev": "2-4705a219cdcca7c72aac4f623f5c46a8",
"_attachments": {
"LFC.jpg": {
"content_type": "image/jpg",
"revpos": 2,
"digest": "md5-9Swz8jvmga5mfBIsmCxCtQ==",
"length": 91408,
"stub": true
}
}
}



Visit :


Discussion



* You must be logged in to add comment.