cURL / Mailing Lists / curl-library / Single Mail


How to POST file with filename?

From: CP YEH <>
Date: Thu, 15 Oct 2009 16:55:52 -0400


I am trying to use libcurl to post a file and what I want to do is
that I want to give filename and file content to the server so the
server can save file according to the name I give.
For instance, if I say "location=abc.txt&data=abc", the server will
create abc.txt file and write abc to the abc.txt file.

I can do the above fairly easily by following examples but what I
really want to do is that I want to mmap file and simple pass it as an
argument to libcurl.
I am just not sure how I can pass mmap file together with "location=abc.txt"

So I wrote the code as follows.

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL,

        // ignore self-signed certificate
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(buf) + fsize);

        // Now specify the POST data
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fmap);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        if (res != 0) {
            printf("curl_easy_perform failed with %d\n", res);

        /* always cleanup */

Currently server is failing to create a file so I wanted to ask what
went wrong. Please give me some advice to sort out the issue. Or if
you think my approach is not good, please tell me other alternatives.
Thank you.

List admin:
Received on 2009-10-15