You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Jonathan Hall <fl...@flimzy.com> on 2017/12/01 15:56:35 UTC

How to properly PUT with multipart/related

I'm trying to do a PUT with a multipart/related body to upload multiple 
attachments at once, and am having quite some difficulty.

I have sent the following request to CouchDB 2.1.1:

curl -v -X PUT http://admin:abc123@localhost:6002/foo/x -H 
'Content-Type: multipart/related; 
boundary=a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6' -d @-

Then pasted the following body (created using Go's multipart mime library):

--a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6
Content-Type: application/json

{"_attachments":{"foo.txt":{"content_type":"text/plain","follows":true}}
,"feet":4}

--a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6
Content-Disposition: attachment; filename="foo.txt"
Content-Type: text/plain

test content

--a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6--

And CouchDB responds with the following error:

< HTTP/1.1 500 Internal Server Error
< X-CouchDB-Body-Time: 0
< X-Couch-Request-ID: ac15e3279d
< Server: CouchDB/2.1.1 (Erlang OTP/17)
< Date: Fri, 01 Dec 2017 15:48:48 GMT
< Content-Type: application/json
< Content-Length: 561
< Connection: close
< Cache-Control: must-revalidate
<
{"error":"error","reason":"{{nocatch,<<\"expected more data\">>},\n 
[{chttpd_db,receive_request_data,2,[{file,\"src/chttpd_db.erl\"},{line,932}]},\n 
{couch_httpd,read_until,3,[{file,\"src/couch_httpd.erl\"},{line,1062}]},\n 
{couch_httpd,parse_part_header,1,[{file,\"src/couch_httpd.erl\"},{line,1090}]},\n 
{couch_httpd,parse_multipart_request,3,\n 
[{file,\"src/couch_httpd.erl\"},{line,1027}]},\n 
{couch_httpd_multipart,'-decode_multipart_stream/3-fun-1-',4,\n 
[{file,\"src/couch_httpd_multipart.erl\"},{line,33}]}]}"}

What "more data" is it expecting?

The documentation on this feature is somewhat sparse, and I expect 
outdated: 
http://docs.couchdb.org/en/2.1.1/api/document/common.html#creating-multiple-attachments

I have also tried using the exact data provided in the documentation 
example, and I get the same error.

Can anyone point me to what I might be doing wrong?

-- Jonathan


Re: How to properly PUT with multipart/related

Posted by Jonathan Hall <fl...@flimzy.com>.
I have found the solution:

1. I must use --data-binary instead of -d (thanks to Johannes J. Schmidt)
2. I must include the "length" field in each "_attachments" object.


On 12/01/2017 05:56 PM, Johannes Jörg Schmidt wrote:
> Hi Jonathan,
>
> I think you need to add the appropriate Content-Length header.
>
> Greetings
> Johannes
>
> Am 01.12.2017 16:56 schrieb "Jonathan Hall" <fl...@flimzy.com>:
>
> I'm trying to do a PUT with a multipart/related body to upload multiple
> attachments at once, and am having quite some difficulty.
>
> I have sent the following request to CouchDB 2.1.1:
>
> curl -v -X PUT http://admin:abc123@localhost:6002/foo/x -H 'Content-Type:
> multipart/related; boundary=a2aae72ae96cf8c2d8805
> 04264545dc6379dd10c912a1cd7639118ecf1b6' -d @-
>
> Then pasted the following body (created using Go's multipart mime library):
>
> --a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6
> Content-Type: application/json
>
> {"_attachments":{"foo.txt":{"content_type":"text/plain","follows":true}}
> ,"feet":4}
>
> --a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6
> Content-Disposition: attachment; filename="foo.txt"
> Content-Type: text/plain
>
> test content
>
> --a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6--
>
> And CouchDB responds with the following error:
>
> < HTTP/1.1 500 Internal Server Error
> < X-CouchDB-Body-Time: 0
> < X-Couch-Request-ID: ac15e3279d
> < Server: CouchDB/2.1.1 (Erlang OTP/17)
> < Date: Fri, 01 Dec 2017 15:48:48 GMT
> < Content-Type: application/json
> < Content-Length: 561
> < Connection: close
> < Cache-Control: must-revalidate
> <
> {"error":"error","reason":"{{nocatch,<<\"expected more data\">>},\n
> [{chttpd_db,receive_request_data,2,[{file,\"src/chttpd_db.erl\"},{line,932}]},\n
> {couch_httpd,read_until,3,[{file,\"src/couch_httpd.erl\"},{line,1062}]},\n
> {couch_httpd,parse_part_header,1,[{file,\"src/couch_httpd.erl\"},{line,1090}]},\n
> {couch_httpd,parse_multipart_request,3,\n
> [{file,\"src/couch_httpd.erl\"},{line,1027}]},\n
> {couch_httpd_multipart,'-decode_multipart_stream/3-fun-1-',4,\n
> [{file,\"src/couch_httpd_multipart.erl\"},{line,33}]}]}"}
>
> What "more data" is it expecting?
>
> The documentation on this feature is somewhat sparse, and I expect
> outdated: http://docs.couchdb.org/en/2.1.1/api/document/common.html#cr
> eating-multiple-attachments
>
> I have also tried using the exact data provided in the documentation
> example, and I get the same error.
>
> Can anyone point me to what I might be doing wrong?
>
> -- Jonathan
>


Re: How to properly PUT with multipart/related

Posted by Johannes Jörg Schmidt <sc...@netzmerk.com>.
Hi Jonathan,

I think you need to add the appropriate Content-Length header.

Greetings
Johannes

Am 01.12.2017 16:56 schrieb "Jonathan Hall" <fl...@flimzy.com>:

I'm trying to do a PUT with a multipart/related body to upload multiple
attachments at once, and am having quite some difficulty.

I have sent the following request to CouchDB 2.1.1:

curl -v -X PUT http://admin:abc123@localhost:6002/foo/x -H 'Content-Type:
multipart/related; boundary=a2aae72ae96cf8c2d8805
04264545dc6379dd10c912a1cd7639118ecf1b6' -d @-

Then pasted the following body (created using Go's multipart mime library):

--a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6
Content-Type: application/json

{"_attachments":{"foo.txt":{"content_type":"text/plain","follows":true}}
,"feet":4}

--a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6
Content-Disposition: attachment; filename="foo.txt"
Content-Type: text/plain

test content

--a2aae72ae96cf8c2d880504264545dc6379dd10c912a1cd7639118ecf1b6--

And CouchDB responds with the following error:

< HTTP/1.1 500 Internal Server Error
< X-CouchDB-Body-Time: 0
< X-Couch-Request-ID: ac15e3279d
< Server: CouchDB/2.1.1 (Erlang OTP/17)
< Date: Fri, 01 Dec 2017 15:48:48 GMT
< Content-Type: application/json
< Content-Length: 561
< Connection: close
< Cache-Control: must-revalidate
<
{"error":"error","reason":"{{nocatch,<<\"expected more data\">>},\n
[{chttpd_db,receive_request_data,2,[{file,\"src/chttpd_db.erl\"},{line,932}]},\n
{couch_httpd,read_until,3,[{file,\"src/couch_httpd.erl\"},{line,1062}]},\n
{couch_httpd,parse_part_header,1,[{file,\"src/couch_httpd.erl\"},{line,1090}]},\n
{couch_httpd,parse_multipart_request,3,\n
[{file,\"src/couch_httpd.erl\"},{line,1027}]},\n
{couch_httpd_multipart,'-decode_multipart_stream/3-fun-1-',4,\n
[{file,\"src/couch_httpd_multipart.erl\"},{line,33}]}]}"}

What "more data" is it expecting?

The documentation on this feature is somewhat sparse, and I expect
outdated: http://docs.couchdb.org/en/2.1.1/api/document/common.html#cr
eating-multiple-attachments

I have also tried using the exact data provided in the documentation
example, and I get the same error.

Can anyone point me to what I might be doing wrong?

-- Jonathan