基础

        multipart/form-data其实就是浏览器用表单上传文件的方式。最常见的情境是:在写邮件时,向邮件后添加附件,附件通常使用表单添加,也就是用multipart/form-data格式上传到服务器。

场景

         Http服务器定义了上传数据的格式,接口地址 http://10.10.10.10:80/restful/personInfo,参数如下:
msg:{
   "name" : "fengyuzaitu",
   "data" : {
      "id" : "9191"
   },
   "sex" : "1",
   "type" : "worker"
}


代码

int PostHttpFormDataByLibCurl()
{
 Json::Value root;
 root["type"] = "worker";
 root["sex"] = "1";
 root["name"] = "fengyuzaitu";
 Json::Value data;
 data["id"] = "9191";
 root["data"] = data;

 std::string strPostData = root.toStyledString();

 CURL *pCurlHandle = curl_easy_init();
 std::string strResponseData;
 curl_easy_setopt(pCurlHandle, CURLOPT_CUSTOMREQUEST, "POST");
 curl_easy_setopt(pCurlHandle, CURLOPT_URL, "http://10.10.10.10:80/restful/personInfo");

 curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponseBody);//设置回调函数
 curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//设置回调函数的参数,获取反馈信息
 struct curl_httppost *pFormPost = 0;
 st