5

I start with a document located at localhost:5984/categories/Jan with the following data:

{
  "_id": "Jan",
  "_rev": "4-2c0b1c27daca6d2a3c375b0f879a8967",
  "name": "Jan",
}

I want to upload a pdf to this document, so I give the following curl command:

curl -vX PUT 'http://localhost:5984/categories/Jan/example.pdf?rev=4-2c0b1c27daca6d2a3c375b0f879a8967' -d@example.pdf -H "ContentType: application/pdf"

And I get this response:

*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 5984 (#0)
> PUT /categories/Jan/example.pdf?rev=4-2c0b1c27daca6d2a3c375b0f879a8967 HTTP/1.1
> Host: localhost:5984
> User-Agent: curl/7.47.0
> Accept: */*
> ContentType: application/pdf
> Content-Length: 10944067
> Content-Type: application/x-www-form-urlencoded
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
< Server: MochiWeb/1.0 (Any of you quaids got a smint?)
< Date: Thu, 11 Jan 2018 07:48:21 GMT
< Connection: close
* We are completely uploaded and fine
< HTTP/1.1 201 Created
< X-CouchDB-Body-Time: 0
< X-Couch-Request-ID: d509f366b8
< Server: CouchDB/2.1.1 (Erlang OTP/18)
< Location: http://localhost:5984/categories/Jan/example.pdf
< Date: Thu, 11 Jan 2018 07:48:23 GMT
< Content-Type: application/json
< Content-Length: 66
< Cache-Control: must-revalidate
<
{"ok":true,"id":"Jan","rev":"5-4ae0ca0bd54d63b9ac45e3e57785648d"}

This appears to me to be a successful upload. However, when I visit the url localhost:5984/categories/Jan/example.pdf, there are two problems:

  • instead of opening in the browser, I'm asked to download the attachment
  • when I download and open the attachment, it says the data can't be read.

This happens for other content-types besides pdfs (I've also tried w/ a png). Can anyone let me know what might be going wrong? Thanks.

David J.
  • 1,753
  • 13
  • 47
  • 96
  • Which version are you using? after the load do you have an "_attachments" attribute in the document? – Juanjo Rodriguez Jan 15 '18 at 08:31
  • Sorry, I see in the trace that you are using last version 2.1.1 – Juanjo Rodriguez Jan 15 '18 at 08:47
  • @JuanjoRodríguez I'm also using the same code on another computer (a Mac) that must use version 1 of Couch. Files upload successfully now, but when I try to open them in the browser the file doesn't open properly. Is "_attachments" necessary for version 1? Could you give an example of a curl command with a data binary body that uses it successfully? Thanks for the help. – David J. Jan 15 '18 at 09:12
  • I think so, it is the way that the attachment is stored in the document: http://docs.couchdb.org/en/1.6.1/api/document/common.html#attachments – Juanjo Rodriguez Jan 15 '18 at 09:25

1 Answers1

3

The problem seems to be the ContentType header that you have specified during the attachment creation.

The correct header name is Content-Type

The curl command should be:

curl -vX PUT 'http://localhost:5984/categories/Jan/example.pdf?rev=4-2c0b1c27daca6d2a3c375b0f879a8967' -d@example.pdf -H "Content-Type: application/pdf"
Juanjo Rodriguez
  • 2,103
  • 8
  • 19