cURL / Mailing Lists / curl-library / Single Mail

curl-library

Cannot get it work with 'CURLFORM_FILECONTENT'

From: Ryan <g-5_at_163.com>
Date: Tue, 30 Jul 2013 16:57:58 +0800 (CST)

Hi all

I am trying libcurl to write something on raspberryPI. I write a simple code to attach a file in the POST request. Here I hit a problem with 'CURLFORM_FILECONTENT' while using curl_formadd() function. Here is the the code.

        CURL *curl = NULL;
        CURLcode ret;
        struct curl_httppost *post = NULL;
        struct curl_httppost *last = NULL;

        FILE* tmp_body_file = tmpfile();
        FILE* tmp_header_file = tmpfile();

        curl_formadd(&post, &last, CURLFORM_PTRNAME, "access_token", CURLFORM_PTRCONTENTS, access_token, CURLFORM_END);
        curl_formadd(&post, &last, CURLFORM_PTRNAME, "status", CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
        curl_formadd(&post, &last, CURLFORM_PTRNAME, "pic", CURLFORM_FILECONTENT, picture_file_name, CURLFORM_END);

        curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_URL, WEIBO_UPLOAD_URL);
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
        curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, tmp_body_file);
        curl_easy_setopt(curl, CURLOPT_WRITEHEADER, tmp_header_file);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        ret = curl_easy_perform(curl);
        curl_formfree(post);
        curl_easy_cleanup(curl);

I just want libcurl to read the file content and add it in the post body. However the the request doesn't have the file content as expected, and I confirmed by doing tcpdump.

While I change the code like this. (only change this line with CURLFORM_FILE option, other arguments stay the same)
        curl_formadd(&post, &last, CURLFORM_PTRNAME, "pic", CURLFORM_FILE, picture_file_name, CURLFORM_END);

It uses CURLFORM_FILE to add the file as a file upload part, then the I could see the file content in the the POST body. So this proves libcurl could read the file, and it's not the trouble with the target file, i.e permission or somethikng else

I am curious whether it's my wrong usage with CURLFORM_FILECONTENT option, or it's caused by other reason? Could anyone help and share the insight?

Thanks a lot!

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-07-30