Attachments are not replicated to CouchDB from Couchbase Lite 1.2.0/1.2.1

Hi,

I’m trying to add attachment in Couchbase Lite to a database and replicate it to CouchDB, but it fails. CouchDB says invallid_json with reason garbage_after_value that seems to be true. Here is the log from CouchDB:

[Mon, 04 Jul 2016 14:06:01 GMT] [debug] [<0.16087.0>] ‘POST’ /my_db_aa67909b6f17483ab90efe99f5852490/_revs_diff {1,
1} from “10.42.0.200”
Headers: [{‘Accept’,“multipart/related, application/json”},
{‘Accept-Encoding’,“gzip, deflate”},
{‘Connection’,“Keep-Alive”},
{‘Content-Length’,“130”},
{‘Content-Type’,“application/json”},
{‘Host’,“10.42.0.1:5984”},
{‘User-Agent’,“CouchbaseLite/1.2 (1.2.0/3fa390b596672fa39243cb9f3457c276eb53707b)”}]
[Mon, 04 Jul 2016 14:06:01 GMT] [debug] [<0.16087.0>] OAuth Params:
[Mon, 04 Jul 2016 14:06:01 GMT] [info] [<0.16087.0>] 10.42.0.200 - - POST /my_db_aa67909b6f17483ab90efe99f5852490/_revs_diff 200
[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16091.0>] ‘POST’ /my_db_aa67909b6f17483ab90efe99f5852490/_bulk_docs {1,
1} from “10.42.0.200”
Headers: [{‘Accept’,“multipart/related, application/json”},
{‘Accept-Encoding’,“gzip, deflate”},
{‘Connection’,“Keep-Alive”},
{‘Content-Length’,“296”},
{‘Content-Type’,“application/json”},
{‘Host’,“10.42.0.1:5984”},
{‘User-Agent’,“CouchbaseLite/1.2 (1.2.0/3fa390b596672fa39243cb9f3457c276eb53707b)”}]
[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16091.0>] OAuth Params:
[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16090.0>] ‘PUT’ /my_db_aa67909b6f17483ab90efe99f5852490/1467641162858_37cb750b-aaeb-4562-a47c-10ec255a83da?new_edits=false {1,
1} from “10.42.0.200”
Headers: [{‘Accept’,“/”},
{‘Connection’,“Keep-Alive”},
{‘Content-Type’,“multipart/related; boundary=LTn8FIwowbJZs5GP15ifqWfuZlcrQhX6AoM”},
{‘Host’,“10.42.0.1:5984”},
{‘Transfer-Encoding’,“chunked”}]
[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16090.0>] OAuth Params: [{“new_edits”,“false”}]
[Mon, 04 Jul 2016 14:06:02 GMT] [info] [<0.16091.0>] 10.42.0.200 - - POST /my_db_aa67909b6f17483ab90efe99f5852490/_bulk_docs 201
[Mon, 04 Jul 2016 14:06:02 GMT] [error] [<0.16090.0>] attempted upload of invalid JSON (set log_level to debug to log it)
[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16090.0>] Invalid JSON: {{error,garbage_after_value},
[<<“{"_rev":"2-b1314c55c2ab85a685a6bb7fecfe07c3","layer_id":"1467641162861_0cc68d42-36e9-4c38-87e7-327442747555","_attachments":{"image":{"digest":"sha1-H/xAix2xpGGRmEoHGhf+uqzoZww=","length":3118398,"follows":true,"revpos":2,"content_type":"image"}},"height":"4032","width":"3024","_revisions":{"start":2,"ids":["b1314c55c2ab85a685a6bb7fecfe07c3","74d53f450e539e5f675c0ee883dd67db"]},"_id":"1467641162858_37cb750b-aaeb-4562-a47c-10ec255a83da"}\r\n1096\r\n”>>]}
[Mon, 04 Jul 2016 14:06:02 GMT] [info] [<0.16090.0>] 10.42.0.200 - - PUT /my_db_aa67909b6f17483ab90efe99f5852490/1467641162858_37cb750b-aaeb-4562-a47c-10ec255a83da?new_edits=false 400
[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16090.0>] httpd 400 error response:
{“error”:“bad_request”,“reason”:“invalid_json”}

[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16061.0>] ‘GET’ /my_db_aa67909b6f17483ab90efe99f5852490/_changes?feed=longpoll&limit=50&heartbeat=300000&style=all_docs&since=569 {1,
1} from “10.42.0.200”
Headers: [{‘Connection’,“Keep-Alive”},{‘Host’,“10.42.0.1:5984”}]
[Mon, 04 Jul 2016 14:06:02 GMT] [debug] [<0.16061.0>] OAuth Params: [{“feed”,“longpoll”},
{“limit”,“50”},
{“heartbeat”,“300000”},
{“style”,“all_docs”},
{“since”,“569”}]
[Mon, 04 Jul 2016 14:06:02 GMT] [info] [<0.16061.0>] 10.42.0.200 - - GET /my_db_aa67909b6f17483ab90efe99f5852490/_changes?feed=longpoll&limit=50&heartbeat=300000&style=all_docs&since=569 200

My CouchDB version 1.6.0.
I reproduced the issue with Couchbase Lite 1.2.0 and 1.2.1. Than I returned back to version 1.1.0 and the issue is gone.

What platform? We have three different implementations of Couchbase Lite.

Looks like the JSON is
{\"rev\":\"2-b1314c55c2ab85a685a6bb7fecfe07c3\",\"layerid\":\"1467641162861_0cc68d42-36e9-4c38-87e7-327442747555\",\"attachments\":{\"image\":{\"digest\":\"sha1-H/xAix2xpGGRmEoHGhf+uqzoZww=\",\"length\":3118398,\"follows\":true,\"revpos\":2,\"contenttype\":\"image\"}},\"height\":\"4032\",\"width\":\"3024\",\"revisions\":{\"start\":2,\"ids\":[\"b1314c55c2ab85a685a6bb7fecfe07c3\",\"74d53f450e539e5f675c0ee883dd67db\"]},\"id\":\"1467641162858_37cb750b-aaeb-4562-a47c-10ec255a83da\"}\r\n1096\r\n

The \r\n1096\r\n looks like HTTP chunked-mode encoding, and the request headers do include Transfer-Encoding: chunked. I recall that CouchDB has trouble with chunked encoding in some circumstances.

Sorry, it’s Android.

@hideki, are there any known compatibility issues with the multipart code in Java?

I looked through the Cocoa source and found a reference to the CouchDB bug: COUCHDB-1403. It’s marked as closed, but just because it got duplicated to another bug, COUCHDB-1956. That other bug is still open, so the problem has not been fixed … four years after I reported it. :-p

PS: The reference is at CBLMultipartUploader.m:45. The code there makes sure the content length is known, and sets an explicit Content-Length header so the HTTP encoder won’t switch to chunked mode.