CURL - 未知表单选项

时间:2014-10-10 18:48:20

标签: c++ curl libcurl

我正在尝试发出HTTP POST请求,我有两个文本输入和3个要上传的文件

当我尝试使用

添加到表单时,我似乎无法上传文件
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "brush", CURLFORM_FILE, brush_image, CURLFORM_END);

函数的返回值是CURL_FORMADD_UNKNOWN_OPTION,我无法弄清楚我做错了什么,这是我的代码

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);

1 个答案:

答案 0 :(得分:2)

您正在将std::string个变量传递给curl_formadd()。它没有std::string的概念,只有char*。您可以使用std::string::c_str()方法传递char*值:

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);