我尝试使用发布的答案,但发现它不适用于 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