C ++ 14 cURLcpp提交表单

时间:2015-12-30 19:08:15

标签: c++ libcurl c++14

我正在尝试使用cURLcpp (not cURLpp)提交表单。在自述文件中有一个如何发送表单请求的示例。这是我的代码:

const std::string authenticityToken = stringMatch.substr(7, stringMatch.length());

curl::curl_form form;
curl::curl_easy easy;
// Forms creation
curl::curl_pair<CURLformoption,std::string> nameForm(CURLFORM_COPYNAME, "username");
curl::curl_pair<CURLformoption,std::string> nameCont(CURLFORM_COPYCONTENTS, "the username");

curl::curl_pair<CURLformoption,std::string> passForm(CURLFORM_COPYNAME, "password");
curl::curl_pair<CURLformoption,std::string> passCont(CURLFORM_COPYCONTENTS, "the password");

curl::curl_pair<CURLformoption,std::string> authForm(CURLFORM_COPYNAME,"authenticityToken");
curl::curl_pair<CURLformoption,std::string> authCont(CURLFORM_COPYCONTENTS, authenticityToken);

try {
    // Form adding
    form.add(nameForm, nameCont);
    form.add(passForm, passCont);
    form.add(authForm, authCont);

    // Add some options to our request
    easy.add<CURLOPT_URL>("https://account.mojang.com/login");
    easy.add<CURLOPT_SSL_VERIFYPEER>(false);
    easy.add<CURLOPT_FOLLOWLOCATION>(1L);
    easy.add<CURLOPT_HTTPPOST>(form);
    // Execute the request.
    easy.perform();
} catch (curl::curl_easy_exception error) {
    // If you want to get the entire error stack we can do:
    curl::curlcpp_traceback errors = error.get_traceback();
    // Otherwise we could print the stack like this:
    error.print_traceback();
    // Note that the printing the stack will erase it
}

编译时出现此错误:

C:\Users\Czarek\ClionProjects\Learning\main.cpp: In function 'int main(int, const char**)':
C:\Users\Czarek\ClionProjects\Learning\main.cpp:40:40: error: no matching function for call to 'curl::curl_easy::add(curl::curl_form&)'
         easy.add<CURLOPT_HTTPPOST>(form);

现在,我按照github上概述的示例进行了操作?我做错了什么?

2 个答案:

答案 0 :(得分:0)

很明显,curl_easy类没有接受add()的{​​{1}}版本。看起来只有它具有CURLOption的add()变体。

答案 1 :(得分:0)

我明白了。事实证明,网站上的教程有点不对劲。 easy.add<OPT>(value)函数接受curl_httpost变量。要从curl_form获取它,您必须form.get()。但要将其传递给easy.add()函数,您必须使用const_cast删除const。我相信这只是图书馆的一个错误。