如何使用curlpp通过POST方法上传文件和json数据
How to upload file and json data with method POST use curlpp
我想使用curlpp库编写C++代码,当然,如果可能的话,它的工作方式与下面的curl示例完全一样。
> curl -H "Content-Type: application/json" -X POST -d '{"param1":"val1", "param2":"val2", "param3":"val3"}' --data-binary '@/tmp/somefolder/file.bin' https://my-api.somedomain.com:1024/my_command_url
>
我可以用POST方法编写transfer-json文本,但当我添加upload命令时,这个库代替了PUT而不是POST。
我决定在这里发布答案,也许这会帮助像我这样的人
static string post_request(const string url,const string body1,const string path2file)
{
const string field_divider="&";
stringstream result;
try
{
using namespace std;
// This block responsible for reading in the fastest way media file
// and prepare it for sending on API server
ifstream is(path2file);
is.seekg(0, ios_base::end);
size_t size=is.tellg();
is.seekg(0, ios_base::beg);
vector<char> v(size/sizeof(char));
is.read((char*) &v[0], size);
is.close();
string body2(v.begin(),v.end());
// Initialization
curlpp::Cleanup cleaner;
curlpp::Easy request;
list< string > headers;
headers.push_back("Content-Type: application/json");
headers.push_back("User-Agent: curl/7.77.7");
using namespace curlpp::Options;
request.setOpt(new Verbose(true));
request.setOpt(new HttpHeader(headers));
request.setOpt(new Url(url));
request.setOpt(new PostFields(body1+field_divider+body2));
request.setOpt(new PostFieldSize(body1.length()+field_divider.length()+body2.length()));
request.setOpt(new curlpp::options::SslEngineDefault());
request.setOpt(WriteStream(&result));
request.perform();
}
catch ( curlpp::LogicError & e )
{
cout << e.what() << endl;
}
catch ( curlpp::RuntimeError & e )
{
cout << e.what() << endl;
}
return (result.str());
}
我尝试使用发布的答案,但发现它不能很好地与express的json解析器配合使用,每次都会发出错误的请求。我认为使用curlpp的表单数据可能是最好的选择。有关发送json字符串和格式为的文件的基本示例,请参阅下面的代码
std::string BasicFormDataPost(std::string url, std::string body1, std::string filename)
{
std::ostringstream result;
try
{
// Initialization
curlpp::Cleanup cleaner;
curlpp::Easy request;
curlpp::Forms formParts;
formParts.push_back(new curlpp::FormParts::Content("formjson",body1)); // One has to remember to JSON.parse on the server to use the body data.
formParts.push_back(new curlpp::FormParts::File("attachment", filename));
using namespace curlpp::Options;
// request.setOpt(new Verbose(true));
request.setOpt(new Url(url));
request.setOpt(new HttpPost(formParts));
request.setOpt(WriteStream(&result));
request.perform();
return std::string( result.str());
}
catch ( curlpp::LogicError & e )
{
std::cout << e.what() << std::endl;
}
catch ( curlpp::RuntimeError & e )
{
std::cout << e.what() << std::endl;
}
}
这个答案是根据上一个答案和curlpp示例拼凑而成的,这些示例位于:https://github.com/datacratic/curlpp/tree/master/examples
相关文章:
- 如何使用curlpp通过POST方法上传文件和json数据
- 如何在特定位置追加 json 文件
- 将 FlatBuffer 对象序列化为 JSON,而不使用其架构文件
- 如何在Visual C+++中从JSON文件中读取节点数据
- 如何将 json 数据附加到 Qt 中的现有 json 文件中
- 当前步骤:构建任务.json 文件
- 可以在JSON文件的帮助下在Qt中创建UI表单
- 将 cpprestsdk json 值对象写入文件
- 如何在 C++ 中使用提升属性树从 JSON 文件中读取对象数组
- 有没有办法从Makefile访问VS Code c_cpp_properties.json文件中的定义
- 保存json文件后如何返回文件路径
- 构造一个类似于JSON文件c++的字符串
- 使用 jsoncpp 读取 JSON 文件
- 在C++中打开 json 文件时遇到问题
- 使用 JSONCPP 将 JSON 数据增量写入文件
- Nlohmann 在不知道密钥的情况下解析 JSON 文件
- 如何使用 rapidjson 读取 json 文件并输出到 std::string?
- 比较 JSON::Value 变量中的数据,然后更新到文件
- 如何使用QJSONARRAY在QT5(C )中解析JSON文件(数组)
- 在 c++ 中读取配置文件 json 并没有停止