Attachment in a document on Sync gateway using .Net console application


We are trying to create a utility where the data is taken from legacy system converted into JSON and put onto sync gateway which is having a couch base bucket connected to it. All well went until its about attachment.

We are using HttpClient of .net and using rest API of sync gateway. The problem is, we are able to put document by converting image in base64 string content. but when we try get call using fiddler the image doesn’t come back. It says Not an image [image/png]. Ad following is the raw data :- HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 26548
Content-Type: image/png;
Etag: "sha1-h6+6oSZDArBAkLPLYHdulQ03XDM="
Server: Couchbase Sync Gateway/1.4
Date: Tue, 02 May 2017 09:46:49 GMT


Below is the code we are using.

var row = objDataSet.Tables[0].Rows[i];
var url = string.Concat(RetrieveUrl(), row[columnName1].ToString(), “/”, row[columnName3].ToString());
var file = Image.FromFile(row[columnName2].ToString());
string content;
using (var strm = new MemoryStream())
file.Save(strm, file.RawFormat);
content = Convert.ToBase64String(strm.ToArray());

                    PostData(url, content);

private static void PostData(string url, string base64Content)
if (string.IsNullOrWhiteSpace(url) || string.IsNullOrWhiteSpace(base64Content))

        using (var client = new HttpClient())
            var content = new StringContent(base64Content,UTF8Encoding.UTF8,"image/png");
            var response = Task.Run(async () => await client.PutAsync(url, content)).Result;
            Console.WriteLine(response.StatusCode + "  " + url);

Is there other way using .net to add photograph to couchbase via syn gateway.

which API do you use?
you should use this API to add attachment.

I am using this API only. Is there an issue with the length of file name?

This code is creating URL which is http://localhost:4984/db/docid/imagename

What I am doing is keeping imagename = docid.fileextension of original file. which works fine when I put it by using fiddler. Content type also looks correct but don’t know where is the issue?

The attachment name must be a URL-encoded string (the file name).
the Content-Type headers (to set the attachment content type).

Following the URL created :-

I think its correct

.[quote=“pankaj.sharma, post:1, topic:12668”]
Accept-Ranges: bytesContent-Length: 26548Content-Type: image/png;Etag: "sha1-h6+6oSZDArBAkLPLYHdulQ03XDM="Server: Couchbase Sync Gateway/1.4Date: Tue, 02 May 2017 09:46:49 GMT

The content type also looks correct to me.


You can use curl to see how to add binary attachments using curl, then you can compare with the request generated from from your .net code.

Assuming you have a JPG file on your local client called attach-test.jpg, and you have a document already stored in a DB in Sync Gateway called doc1 at rev1, the following curl command should add the image as an attachment:

curl -vX PUT --data-binary @attach-test.jpg -H "Content-Type: image/jpg"

Then you should see a response similar to the following if you get the document back:

{ "_id":"doc1", "_rev":"2-20682f5bd641ae158e70af9ad1fb79ae", "foo":"bar", "_attachments":{ "attach-test.jpg":{ "content_type":"image/jpg", "digest":"sha1-u1ey9gajINpb/O3m24YXq0Oc6As=", "length":98623, "revpos":2, "stub":true } } }

before SG 1.3.1 (1.3.0), the attachment API with public port works fine.
the same request for SG 1.3.1 and SG 1.4 with public port, it return the following error:

  "error": "not_found",
  "reason": "Document revision is not accessible"

but with the admin port works fine.