Hi team,
we need to upload a attachment through sync gateway - admin attachment rest api method. Is there a way we can create the document and attach an image in one go? what will be request model and how do we attach an image in this case.
we are able to create a document using PUT and then attach a document but can we do it in 1 call? any example will be really useful.
below works but document internal representation is different format than previous method.
{
“_attachments”: {
“sample.png”: {
“data”: “XXXX”
}
},
“Id”: “TEST_REQ19”,
“$Type” : “Transaction”
}
If you are not base64 encoding the attachment/binary data, then you you’ll want to use multipart/related
and you’ll also need to ensure that the first part (the document body) includes the _attachments property describing the following attachment MIME part.
It’s not as straightforward as the option where you create a document and then add an attachment because you would need to calculate the digest and also get the structure of the _attachments property right.
So you are better off creating doc and then adding attachment. Or do base64 encoded version.
This is the doc data when we upload the image from cblite java SDK(mainly looks at the content_type,@type).
{
“$Type”: “Transaction”,
“docAttachment”: {
“content_type”: “image/png”,
“@type”: “blob”,
“digest”: “sha1-vfz4RNYdcogirIzSVuUTGqKVFmw=”,
“length”: 36418
},
“Id”: “TEST_REQ31”,
“_rev”: “1-9f151ddf3802f54a96b76648fe6d890488be38ac”,
“id": “TEST_REQ31”
}
in CB under meta data,
“attachments”: {
"blob/docAttachment”: {
“digest”: “sha1-vfz4RNYdcogirIzSVuUTGqKVFmw=”,
“revpos”: 1,
“content_type”: “image/png”,
“length”: 36418,
“stub”: true
}
},
same thing if upload via sync gw - rest api,
{
“$Type”: “Transaction”,
“Id”: “TEST_REQ32”,
“_rev”: “2-9e8b2dfa30a723aacbca43bcb368bf2c”,
“_id”: “TEST_REQ32”
}
if you check the CB document for the TEST_REQ32, under meta data, we can see
“cas”: “0x0000f76728247116”,
“value_crc32c”: “0xbbe25eb4”,
“attachments”: {
“XXXXXX.png”: {
“digest”: “sha1-ZmjPHrvPt83G0YDOK9ebcOB1fhI=”,
“revpos”: 2,
“content_type”: “multipart/form-data; boundary=--------------------------708372256808616454994018”,
“length”: 470626,
“stub”: true
}
},
“time_saved”: “2021-03-30T08:11:37.163727532-06:00”
}
Ah ok. You’re seeing some CBL 1.x/SG and CBL 2.x differences, which Sync Gateway and Couchbase Lite both handle differently, but are ultimately still compatible with each other.
There’s more detail in this blog post under the “But…wait…a mismatch!” heading.