Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Libcurl lesson 7 use of multipart/formdata forms

Shulou Source: shulou.com Published: 2022-06-02 19:21:07 10月04日 Update

Scene

Multipart/form-data is the way browsers upload files with forms. The most common scenario is that when writing an email, an attachment is added to the message, usually using a form, that is, uploading it to the server in multipart/form-data format. The Http server defines the format of uploaded data. The API address is http://10.10.10.10:80/restful/personInfo. The parameters are as follows:

Msg: {

"name": "fengyuzaitu"

"data": {

"id": "9191"

}

"sex": "1"

"type": "worker"

}

Code

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); / / set callback function

Curl_easy_setopt (pCurlHandle, CURLOPT_WRITEDATA, & strResponseData); / / set the parameters of the callback function to get feedback information

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

Tags: Functions forms parameters methods code information data servers formats mail attachments services that is memory can be passed address scenario remarks common situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS vpn Shulou Tech Info OPPO Reno Shulou Information