C++ CURL-未知形式选项
我正在尝试发出HTTP POST请求,我有两个文本输入和3个文件要上传 当我尝试使用将文件添加到表单时,似乎无法获取要上载的文件C++ CURL-未知形式选项,c++,curl,libcurl,C++,Curl,Libcurl,我正在尝试发出HTTP POST请求,我有两个文本输入和3个文件要上传 当我尝试使用将文件添加到表单时,似乎无法获取要上载的文件 curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "brush", CURLFORM_FILE, brush_image, CURLFORM_END); 函数的返回值是CURL\u FORMADD\u UNKNOWN\u OPTION,我不知道我做错了什么,这是我的代码 CURL *curl;
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "brush", CURLFORM_FILE, brush_image, CURLFORM_END);
函数的返回值是CURL\u FORMADD\u UNKNOWN\u 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);
您正在将
std::string
变量传递到curl\u 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);
什么是
brush\u image
声明的,它在哪里初始化?或者您使用的是不支持CURLFORM\u文件的较旧版本的curl
?此外,如果curl\u easy\u init()
失败,您正在泄漏formpost
。brush\u图像的类型是std::string,它在较早的时候初始化,它有一个值,我用调试器验证过。将字符串更改为char*似乎有效,知道为什么吗?因为curl\u formadd()
需要一个char*
,所以它没有std::string
的概念(记住,curl是一个基于C的API,C中没有std::string
)。由于brush\u image
是一个std::string
,因此在将其传递到curl\u formadd()时使用brush\u image.c\u str()
。