Aviran
Aviran

Reputation: 5458

CURL - Unknown form option

I'm trying to make an HTTP POST request, I have two text inputs and 3 files to be uploaded

I can't seem to get the files to upload, when I try to add the to the form using

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "brush", CURLFORM_FILE, brush_image, CURLFORM_END);

The return value of the function is CURL_FORMADD_UNKNOWN_OPTION, I cant figure out what am I doing wrong, here's my code

CURL *curl;
CURLcode res;

struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;   
static const char buf[] = "Expect:";

curl_global_init(CURL_GLOBAL_ALL);  


curl_formadd(&formpost,&lastptr,CURLFORM_COPYNAME, "letter", CURLFORM_COPYCONTENTS, "Letter A",CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "points", CURLFORM_COPYCONTENTS, "a b c", CURLFORM_END);

// these call return the unknown option
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "brush", CURLFORM_FILE, brush_image, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "lines", CURLFORM_FILE, lines_image, CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "downsample", CURLFORM_FILE, downsample_image, CURLFORM_END);


curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END);

curl = curl_easy_init();

if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    res = curl_easy_perform(curl);
    if (res != CURLE_OK) {
        // does not go here...
    }
    curl_easy_cleanup(curl);
    curl_formfree(formpost);

Upvotes: 1

Views: 1307

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595295

You are passing std::string variables to curl_formadd(). It has no concept of std::string, only char*. You can use the std::string::c_str() method to pass char* values:

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "brush", CURLFORM_FILE, brush_image.c_str(), CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "lines", CURLFORM_FILE, lines_image.c_str(), CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "downsample", CURLFORM_FILE, downsample_image.c_str(), CURLFORM_END);

Upvotes: 2

Related Questions