如何使用方法POST上传文件和json数据使用curlpp

时间:2018-10-31 20:38:51

标签: c++ httprequest 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方法编写传输json文本,但是当我添加上载命令时,此库将替换PUT而不是POST。

2 个答案:

答案 0 :(得分:1)

我决定在此处发布答案,也许会对像我这样的人有所帮助

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());

}

答案 1 :(得分:0)

I tried using the posted answer but found that it did not work well with express' json parser and it would give a bad request each time. I think using curlpp's form data is most likely the best option. See the code below for a basic example of sending a json string and a file in the form:

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;
 }

}

This answer was pieced together from the previous answer and the curlpp examples located at: https://github.com/datacratic/curlpp/tree/master/examples