1.4.2. /db/doc/attachment

HEAD /{db}/{docid}/{attname}

Returns the HTTP headers containing a minimal amount of information about the specified attachment. The method supports the same query arguments as the GET /{db}/{docid}/{attname} method, but only the header information (including attachment size, encoding and the MD5 hash as an ETag), is returned.

Parameters:
  • db – Database name
  • docid – Document ID
  • attname – Attachment name
Request Headers:
 
  • If-Match – Document’s revision. Alternative to rev query parameter
  • If-None-Match – Attachment’s base64 encoded MD5 binary digest. Optional
Query Parameters:
 
  • rev (string) – Document’s revision. Optional
Response Headers:
 
Status Codes:

Request:

  1. HEAD /recipes/SpaghettiWithMeatballs/recipe.txt HTTP/1.1
  2. Host: localhost:5984

Response:

  1. HTTP/1.1 200 OK
  2. Accept-Ranges: none
  3. Cache-Control: must-revalidate
  4. Content-Encoding: gzip
  5. Content-Length: 100
  6. Content-Type: text/plain
  7. Date: Thu, 15 Aug 2013 12:42:42 GMT
  8. ETag: "vVa/YgiE1+Gh0WfoFJAcSg=="
  9. Server: CouchDB (Erlang/OTP)

GET /{db}/{docid}/{attname}

Returns the file attachment associated with the document. The raw data of the associated attachment is returned (just as if you were accessing a static file. The returned Content-Type will be the same as the content type set when the document attachment was submitted into the database.

Parameters:
  • db – Database name
  • docid – Document ID
  • attname – Attachment name
Request Headers:
 
  • If-Match – Document’s revision. Alternative to rev query parameter
  • If-None-Match – Attachment’s base64 encoded MD5 binary digest. Optional
Query Parameters:
 
  • rev (string) – Document’s revision. Optional
Response Headers:
 
Response:

Stored content

Status Codes:

PUT /{db}/{docid}/{attname}

Uploads the supplied content as an attachment to the specified document. The attachment name provided must be a URL encoded string. You must supply the Content-Type header, and for an existing document you must also supply either the rev query argument or the If-Match HTTP header. If the revision is omitted, a new, otherwise empty document will be created with the provided attachment, or a conflict will occur.

If case when uploading an attachment using an existing attachment name, CouchDB will update the corresponding stored content of the database. Since you must supply the revision information to add an attachment to the document, this serves as validation to update the existing attachment.

Parameters:
  • db – Database name
  • docid – Document ID
  • attname – Attachment name
Request Headers:
 
  • Content-Type – Attachment MIME type. Default: application/octet-stream Optional
  • If-Match – Document revision. Alternative to rev query parameter
Query Parameters:
 
  • rev (string) – Document revision. Optional
Response JSON Object:
 
  • id (string) – Document ID
  • ok (boolean) – Operation status
  • rev (string) – Revision MVCC token
Status Codes:

Request:

  1. PUT /recipes/SpaghettiWithMeatballs/recipe.txt HTTP/1.1
  2. Accept: application/json
  3. Content-Length: 86
  4. Content-Type: text/plain
  5. Host: localhost:5984
  6. If-Match: 1-917fa2381192822767f010b95b45325b
  7. 1. Cook spaghetti
  8. 2. Cook meatballs
  9. 3. Mix them
  10. 4. Add tomato sauce
  11. 5. ...
  12. 6. PROFIT!

Response:

  1. HTTP/1.1 201 Created
  2. Cache-Control: must-revalidate
  3. Content-Length: 85
  4. Content-Type: application/json
  5. Date: Thu, 15 Aug 2013 12:38:04 GMT
  6. ETag: "2-ce91aed0129be8f9b0f650a2edcfd0a4"
  7. Location: http://localhost:5984/recipes/SpaghettiWithMeatballs/recipe.txt
  8. Server: CouchDB (Erlang/OTP)
  9. {
  10. "id": "SpaghettiWithMeatballs",
  11. "ok": true,
  12. "rev": "2-ce91aed0129be8f9b0f650a2edcfd0a4"
  13. }

DELETE /{db}/{docid}/{attname}

Deletes the attachment with filename {attname} of the specified doc. You must supply the rev query parameter or If-Match with the current revision to delete the attachment.

Parameters:
  • db – Database name
  • docid – Document ID
Request Headers:
 
  • Accept
    • application/json
    • text/plain
  • If-Match – Document revision. Alternative to rev query parameter
Query Parameters:
 
  • rev (string) – Document revision. Required
  • batch (string) – Store changes in batch mode Possible values: ok. Optional
Response Headers:
 
  • Content-Type
    • application/json
    • text/plain; charset=utf-8
  • ETag – Double quoted document’s new revision
Response JSON Object:
 
  • id (string) – Document ID
  • ok (boolean) – Operation status
  • rev (string) – Revision MVCC token
Status Codes:
  • 200 OK – Attachment successfully removed
  • 202 Accepted – Request was accepted, but changes are not yet stored on disk
  • 400 Bad Request – Invalid request body or parameters
  • 401 Unauthorized – Write privileges required
  • 404 Not Found – Specified database, document or attachment was not found
  • 409 Conflict – Document’s revision wasn’t specified or it’s not the latest

Request:

  1. DELETE /recipes/SpaghettiWithMeatballs?rev=6-440b2dd39c20413045748b42c6aba6e2 HTTP/1.1
  2. Accept: application/json
  3. Host: localhost:5984

Alternatively, instead of rev query parameter you may use If-Match header:

  1. DELETE /recipes/SpaghettiWithMeatballs HTTP/1.1
  2. Accept: application/json
  3. If-Match: 6-440b2dd39c20413045748b42c6aba6e2
  4. Host: localhost:5984

Response:

  1. HTTP/1.1 200 OK
  2. Cache-Control: must-revalidate
  3. Content-Length: 85
  4. Content-Type: application/json
  5. Date: Wed, 14 Aug 2013 12:23:13 GMT
  6. ETag: "7-05185cf5fcdf4b6da360af939431d466"
  7. Server: CouchDB (Erlang/OTP)
  8. {
  9. "id": "SpaghettiWithMeatballs",
  10. "ok": true,
  11. "rev": "7-05185cf5fcdf4b6da360af939431d466"
  12. }

1.4.2.1. HTTP Range Requests

HTTP allows you to specify byte ranges for requests. This allows the implementation of resumable downloads and skippable audio and video streams alike. This is available for all attachments inside CouchDB.

This is just a real quick run through how this looks under the hood. Usually, you will have larger binary files to serve from CouchDB, like MP3s and videos, but to make things a little more obvious, I use a text file here (Note that I use the application/octet-stream :header`Content-Type` instead of text/plain).

  1. shell> cat file.txt
  2. My hovercraft is full of eels!

Now let’s store this text file as an attachment in CouchDB. First, we create a database:

  1. shell> curl -X PUT http://127.0.0.1:5984/test
  2. {"ok":true}

Then we create a new document and the file attachment in one go:

  1. shell> curl -X PUT http://127.0.0.1:5984/test/doc/file.txt \
  2. -H "Content-Type: application/octet-stream" -d@file.txt
  3. {"ok":true,"id":"doc","rev":"1-287a28fa680ae0c7fb4729bf0c6e0cf2"}

Now we can request the whole file easily:

  1. shell> curl -X GET http://127.0.0.1:5984/test/doc/file.txt
  2. My hovercraft is full of eels!

But say we only want the first 13 bytes:

  1. shell> curl -X GET http://127.0.0.1:5984/test/doc/file.txt \
  2. -H "Range: bytes=0-12"
  3. My hovercraft

HTTP supports many ways to specify single and even multiple byte ranges. Read all about it in RFC 2616#section-14.27.