C++14 cURLcpp提交表单

C++14 cURLcpp Submitting a form

本文关键字:表单 提交 cURLcpp C++14      更新时间:2023-10-16

我正在尝试使用cURLcpp(而不是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:UsersCzarekClionProjectsLearningmain.cpp: In function 'int main(int, const char**)':
C:UsersCzarekClionProjectsLearningmain.cpp:40:40: error: no matching function for call to 'curl::curl_easy::add(curl::curl_form&)'
         easy.add<CURLOPT_HTTPPOST>(form);

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

很明显,curl_easy类没有任何接受curl_formadd()版本。看起来只有add()变体用于CURLOption。

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