Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to put attachment into document #138

Open
lambda-knight opened this issue Mar 18, 2018 · 1 comment
Open

How to put attachment into document #138

lambda-knight opened this issue Mar 18, 2018 · 1 comment

Comments

@lambda-knight
Copy link

Hello,
I'm trying to put an attachment into the existing document.
My code is as follows:

require 'couchrest'
conf.echo = false
db = CouchRest.database!("http://admin:admin@127.0.0.1:5984/db-test")
case1 = db.save_doc('_id' => 'case1', 'test-name' => 'bmp test')
case1 = db.get('case1')
file1 = IO.read('../data/test.bmp')
db.put_attachment(case1,"test",file1 , :content_type => "image/bmp")

The case1 document is as follows:

{
  "_id": "case1",
  "_rev": "19-d073249b4be05d0c00940b9a0dad3830",
  "test-name": "bmp test",
  "_attachments": {
    "test": {
      "content_type": "text/plain",
      "revpos": 19,
      "digest": "md5-Sf9Zgbi3FmNuVS2t+7xfpg==",
      "length": 787510,
      "stub": true
    }
  }
}

The content-type should be 'image/bmp', however it is 'text/plain'.
Then, I tried to fetch the attachment, but it returns 404 error.

data = db.fetch_attachment(case1, "test")
CouchRest::NotFound: 404 Not Found
	from /var/lib/gems/2.3.0/gems/couchrest-2.0.1/lib/couchrest/connection.rb:228:in `raise_response_error'
	from /var/lib/gems/2.3.0/gems/couchrest-2.0.1/lib/couchrest/connection.rb:183:in `handle_response_code'
	from /var/lib/gems/2.3.0/gems/couchrest-2.0.1/lib/couchrest/connection.rb:172:in `send_and_parse_response'
	from /var/lib/gems/2.3.0/gems/couchrest-2.0.1/lib/couchrest/connection.rb:157:in `execute'
	from /var/lib/gems/2.3.0/gems/couchrest-2.0.1/lib/couchrest/connection.rb:67:in `get'
	from /var/lib/gems/2.3.0/gems/couchrest-2.0.1/lib/couchrest/database.rb:336:in `fetch_attachment'
	from (irb):18
	from /usr/bin/irb:11:in `<main>'

Is my code is wrong ? Please let me know how I can put/fetch attachments.

Thanks,

@omensinger
Copy link

You need to provide the document to "fetch_attachment", not just its id. See also https://stackoverflow.com/questions/7606838/standalone-attachments-using-couchrest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants