場景
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--restful
備註
這種表單上傳數據的方式,也能夠經過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;
}app
注意:
std::string strPostUrlEncodeData = "msg=" + strEncodeAuth; 這裏的=不能使用:,不然沒法解析經過curl