如何将 PUT 卷曲映射到 CPP 卷曲?
How to map PUT curl into CPP Curlpp?
我想将下面的 curl(此 curl 在 Linux 终端中正常工作(映射/写入 cpp 代码:
curl -X PUT -u "abc"
"https://api123.pl"
-d 'symbol=EB&side=ri&quantity=3'
以下是使用 Curlpp 库的 CPP 代码:
#include <iostream>
#include <sstream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
using namespace curlpp::options;
using namespace std;
/*
curl -X PUT -u "abc"
"https://api123.pl"
-d 'symbol=EB&side=ri&quantity=3'
g++ name1.cpp -lcurl -lcurlpp -o name1 && ./name1
*/
string test(const std::string &url) {
try {
curlpp::Cleanup cleanup;
curlpp::Easy request;
request.setOpt<curlpp::Options::Url>(url);
request.setOpt(new curlpp::options::UserPwd("abc"));
request.setOpt(new curlpp::options::CustomRequest{"PUT"});
// /*
//does not compile
std::list<std::string> postContent;
postContent.push_back("symbol:EB");
postContent.push_back("side:ri");
postContent.push_back("quantity:3");
request.setOpt(new curlpp::options::PostFields(postContent));
// */
std:stringstream content;
content << request;
return content.str().c_str();
}
catch (const curlpp::RuntimeError &e) {
std::cout << "CURLpp runtime error: " << e.what() << std::endl;
}
catch (const curlpp::LogicError &e) {
std::cout << "CURLpp logic error: " << e.what() << std::endl;
}
throw std::string("Error");
}
int main(int, char **) {
try {
cout << test("https://api123.pl");
}
catch(curlpp::RuntimeError & e) {
std::cout << e.what() << std::endl;
}
catch(curlpp::LogicError & e) {
std::cout << e.what() << std::endl;
}
return 0;
}
我在编译过程中遇到错误:
1.2putStackoverflow.cpp: In function ‘std::string getJSONAPIResultFromURL(const string&)’:
1.2putStackoverflow.cpp:31:67: error: no matching function for call to ‘curlpp::OptionTrait<std::__cxx11::basic_string<char>, CURLOPT_POSTFIELDS>::OptionTrait(std::__cxx11::list<std::__cxx11::basic_string<char> >&)’
31 | request.setOpt(new curlpp::options::PostFields(postContent));
| ^
In file included from /usr/include/curlpp/Option.hpp:251,
from /usr/include/curlpp/Easy.hpp:31,
from 1.2putStackoverflow.cpp:4:
/usr/include/curlpp/Option.inl:129:1: note: candidate: ‘curlpp::OptionTrait<OptionType, opt>::OptionTrait() [with OptionType = std::__cxx11::basic_string<char>; CURLoption opt = CURLOPT_POSTFIELDS]’
129 | OptionTrait<OptionType, option>::OptionTrait()
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/curlpp/Option.inl:129:1: note: candidate expects 0 arguments, 1 provided
/usr/include/curlpp/Option.inl:123:1: note: candidate: ‘curlpp::OptionTrait<OptionType, opt>::OptionTrait(typename curlpp::Option<OptionType>::ParamType) [with OptionType = std::__cxx11::basic_string<char>; CURLoption opt = CURLOPT_POSTFIELDS; typename curlpp::Option<OptionType>::ParamType = const std::__cxx11::basic_string<char>&]’
123 | OptionTrait<OptionType, option>::OptionTrait(typename Option<OptionType>::ParamType value)
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/curlpp/Option.inl:123:85: note: no known conversion for argument 1 from ‘std::__cxx11::list<std::__cxx11::basic_string<char> >’ to ‘curlpp::Option<std::__cxx11::basic_string<char> >::ParamType’ {aka ‘const std::__cxx11::basic_string<char>&’}
123 | OptionTrait<OptionType, option>::OptionTrait(typename Option<OptionType>::ParamType value)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
In file included from /usr/include/curlpp/Easy.hpp:31,
from 1.2putStackoverflow.cpp:4:
/usr/include/curlpp/Option.hpp:145:8: note: candidate: ‘curlpp::OptionTrait<std::__cxx11::basic_string<char>, CURLOPT_POSTFIELDS>::OptionTrait(const curlpp::OptionTrait<std::__cxx11::basic_string<char>, CURLOPT_POSTFIELDS>&)’
145 | class OptionTrait : public Option<OptionType>
| ^~~~~~~~~~~
/usr/include/curlpp/Option.hpp:145:8: note: no known conversion for argument 1 from ‘std::__cxx11::list<std::__cxx11::basic_string<char> >’ to ‘const curlpp::OptionTrait<std::__cxx11::basic_string<char>, CURLOPT_POSTFIELDS>&’
/usr/include/curlpp/Option.hpp:145:8: note: candidate: ‘curlpp::OptionTrait<std::__cxx11::basic_string<char>, CURLOPT_POSTFIELDS>::OptionTrait(curlpp::OptionTrait<std::__cxx11::basic_string<char>, CURLOPT_POSTFIELDS>&&)’
/usr/include/curlpp/Option.hpp:145:8: note: no known conversion for argument 1 from ‘std::__cxx11::list<std::__cxx11::basic_string<char> >’ to ‘curlpp::OptionTrait<std::__cxx11::basic_string<char>, CURLOPT_POSTFIELDS>&&’
问题出在:
std::list<std::string> postContent;
postContent.push_back("symbol:EB");
postContent.push_back("side:ri");
postContent.push_back("quantity:3");
request.setOpt(new curlpp::options::PostFields(postContent));
编译程序:
g++ name1.cpp -lcurl -lcurlpp -o name1 && ./name1
我不知道如何使用curlpp编写代码将curl映射/写入curl到cpp中。谢谢你的帮助。
PostFields
只是cURLpp::OptionTrait<std::string, cURL::CURLOPT_POSTFIELDS>
的一个typedef
,我在下面简化了事情。
如果您仔细阅读错误消息,它们会告诉您到底出了什么问题:
错误:调用 'curlpp::OptionTrait<std::__cxx11::basic_string>list<std::__cxx11::basic_string>
>&('
这基本上是说你不能将std::list<std::string>
传递给PostFields
的构造函数。 错误详细信息中报告了 4 个候选构造函数,其中没有一个将std::list
作为输入:
PostFields()
PostFields(const std::string&)
PostFields(const PostFields&)
PostFields(PostFields&&)
事实上,如果您阅读 cURLpp 文档,则无法从std::list
创建PostFields
。 根据CURLOPT_POSTFIELDS
cURL
文档,这是有道理的:
将 char * 作为参数传递,指向要在 HTTP POST 操作中发送的完整数据。必须确保数据的格式设置为您希望服务器接收数据的方式。libcurl 不会以任何方式为您转换或编码。例如,Web 服务器可能假定此数据是 url 编码的。
CURLOPT_POSTFIELDS
需要单个char*
作为输入(cURLpp 使用std::string
包装(,因此您必须将PUT
数据作为单个 url 编码字符串传递,就像您在命令行上所做的那样,例如:
request.setOpt(new curlpp::Options::PostFields("symbol=EB&side=ri&quantity=3"));
你从哪里得到可以将std::list
与PostFields
一起使用的想法?
顺便说一句,根据 cURLpp 文档,在向curlpp::Easy
对象添加选项时,您实际上不需要使用new
:
curlpp::Easy request;
request.setOpt(curlpp::Options::Url(url));
request.setOpt(curlpp::Options::UserPwd("abc"));
request.setOpt(curlpp::Options::CustomRequest("PUT"));
request.setOpt(curlpp::Options::PostFields("symbol=EB&side=ri&quantity=3"));
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何通过cpp程序运行shell脚本
- 使用2个键的cpp-stl::优先级队列排序不正确
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我需要知道编译器如何在cpp中使用析构函数
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 无法在UE4中包含BP类到CPP类
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 如何将 PUT 卷曲映射到 CPP 卷曲?