cURL / Mailing Lists / curl-library / Single Mail


Re: How to upload file as CouchDB attachment via libCurl

From: Dan Fandrich <>
Date: Mon, 26 May 2014 23:51:19 +0200

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 Im 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.


> 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 cant seem to add a file as an attachment via libcurl (I have
> no issues from the command line), but Im 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:
Received on 2014-05-26