其他分享
首页 > 其他分享> > libcurl第七课 multipart/formdata表单使用

libcurl第七课 multipart/formdata表单使用

作者:互联网

场景
         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;
 struct curl_httppost *pLastPtrFormPost = 0;
 curl_formadd(&pFormPost, &pLastPtrFormPost, CURLFORM_COPYNAME, "msg", CURLFORM_COPYCONTENTS, strPostData.c_str(), CURLFORM_END);
 curl_easy_setopt(pCurlHandle, CURLOPT_HTTPPOST, pFormPost);
 CURLcode nRet = curl_easy_perform(pCurlHandle);
 if (0 == nRet)
 {
  std::cout << strResponseData << std::endl;
 }
 curl_formfree(pFormPost);
 curl_easy_cleanup(pCurlHandle);
 return nRet;
}

报文
POST /restful/personInfo HTTP/1.1
Host: 10.10.10.10:80
Accept: */*
Content-Length: 254
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------2630a8c6c773b062
HTTP/1.1 100
--------------------------2630a8c6c773b062
Content-Disposition: form-data; name="msg"
{
   "name" : "fengyuzaitu",
   "data" : {
      "id" : "9191"
   },
   "sex" : "1",
   "type" : "worker"
}
--------------------------2630a8c6c773b062--

备注
        这种表单上传数据的方式,也可以通过Content-Type: application/x-www-form-urlencoded的方式进行上传
代码
int PostFormDataByUrlEncode()
{
 Json::Value root;
 root["type"] = "worker";
 root["sex"] = "1";
 root["name"] = "fengyuzaitu";
 Json::Value data;
 data["id"] = "9191";
 root["data"] = data;
 std::string strUrl = 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");
 struct curl_slist *pCurlList = NULL;
 //指定文本url编码
 pCurlList = curl_slist_append(pCurlList, "Content-Type: application/x-www-form-urlencoded");
 curl_easy_setopt(pCurlHandle, CURLOPT_HTTPHEADER, headers);
 curl_easy_setopt(pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponseBody);//设置回调函数
 curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//设置回调函数的参数,获取反馈信息
 char* pszEncodeAuth = curl_easy_escape(pCurlHandle, strUrl.c_str(), strUrl.length());
 std::string strEncodeAuth = pszEncodeAuth;
 //释放申请的内存
 curl_free(pszEncodeAuth);
 std::string strPostUrlEncodeData = "msg=" + strEncodeAuth;
 curl_easy_setopt(pCurlHandle, CURLOPT_POSTFIELDS, strPostUrlEncodeData.c_str());
 CURLcode nRet = curl_easy_perform(pCurlHandle);
 std::cout << strResponseData << std::endl;
 curl_slist_free_all(pCurlList);
 curl_easy_cleanup(pCurlHandle);
 return nRet;
}

注意:
 std::string strPostUrlEncodeData = "msg=" + strEncodeAuth; 这里的=不能使用:,否则无法解析通过


标签:setopt,libcurl,root,formdata,第七课,easy,pCurlHandle,curl,data
来源: https://blog.51cto.com/fengyuzaitu/2434781