cURL / Mailing Lists / curl-library / Single Mail


Re: How to upload file as CouchDB attachment via libCurl

From: Lane <>
Date: Mon, 26 May 2014 19:23:59 -0500

Let me clarify. What I consider an attachment, or what CouchDB considers an
attachment is a file to the field. So _attachments is the json objects
field, and the file would be the data to that field. As in my example,
you’ll see the command line …

curl -X POST 'http://localhost:5984/students' -H 'Content-Type:
application/json' -d '{

    "first":"John", "last":"Doe", "age":"20", "_attachments": {

        "john.txt": {

            "content-type": "text/plain",

            "data": "'$(openssl base64 < /tmp/john_file.txt)'"




I have no issues building the json stream. the code that says // build json
stream here. That works, it was just a lot to write out.
So that curl command works on the command line and does exactly what I
want. What I don’t understand is how to upload the part within parenthesis,
the actual file. I.e., $(openssl base64 < filename). This line converts the
file to base64, then curl uploads it. That's the part I don't understand
with the API or to build into the app to upload the file as part of the
json object. And yes, its a huge file.

Thanks for the other stuff, I'll leave out the redundancy as well as the
expect and charset lines. They worked, but I'll try w/o them.

If its worth a look, I was trying to do inline attachments from this link
which allows to add the json object and the attachment.

On Mon, May 26, 2014 at 4:51 PM, Dan Fandrich <>wrote:

> On Mon, May 26, 2014 at 03:49:16PM -0500, Lane wrote:
> > I have been able to input a file using curl from the command line into
> CouchDB,
> > but I’m having an issue adding an attachment to CouchDB using libcurl.
> This
> > command line successfully does what I want. This is what I am trying to
> > duplicate via libcurl.
> >
> > curl -X POST 'http://localhost:5984/students' -H 'Content-Type:
> application/
> > json' -d '{
> The -X option is redundant here.
> [snip]
> > hlist = curl_slist_append(hlist,"Expect: 100-continue");
> And this can be dangerous, as it sets expectations on the receiver that
> libcurl
> won't necessarily honour. Better let libcurl itself handle the
> protocol-level
> work on its own.
> > hlist = curl_slist_append(hlist,"charsets: utf-8");
> This doesn't look like a standard HTTP header; are you sure it's correct?
> > The problem is I can’t seem to add a file as an attachment via libcurl
> (I have
> > no issues from the command line), but I’m not sure how the add the file
> > programmatically in the data field of _attachments here.
> You'll have to clarify what exactly you're trying to accomplish here; HTTP
> has
> no concept of attachments. If you're considering the data field in the JSON
> body an attachment, then you'll have to build it in your app to feed
> libcurl
> in a similar way to how you're building it in the shell to pass to curl on
> the
> command-line. You're on the right track where the code says // build json
> stream here. If it's a huge file that you don't want to build in a
> temporary
> string first, you can pass it in one piece at a time with a
> CURLOPT_READFUNCTION, but you don't have to.
> >>> Dan
> -------------------------------------------------------------------
> List admin:
> Etiquette:

List admin:
Received on 2014-05-27