Whats the effective way of PUT with libcurl. Currently I use the below snippet to POST data to server.
// Pass custom made headers
res = curl_easy_setopt(mCurl.get(), CURLOPT_HTTPHEADER, headers);
if ( res != CURLE_OK )
{
throw MyException();
}
res = curl_easy_setopt(mCurl.get(), CURLOPT_CUSTOMREQUEST, "POST");
if ( res != CURLE_OK )
{
throw MyException();
}
// Set the size of the postfields data
res = curl_easy_setopt(mCurl.get(), CURLOPT_POSTFIELDSIZE, payLoad.length());
if ( res != CURLE_OK )
{
throw MyException();
}
// Now specify the POST data
res = curl_easy_setopt(mCurl.get(), CURLOPT_POSTFIELDS, payLoad.c_str());
if ( res != CURLE_OK )
{
throw MyException();
}
res = curl_easy_perform(mCurl.get());
if ( res != CURLE_OK)
{
throw MyException();
}
else
{
status = rest::SUCCESS;
}
Can I just replace the line curl_easy_setopt(mCurl.get(), CURLOPT_CUSTOMREQUEST, "POST"); with curl_easy_setopt(mCurl.get(), CURLOPT_CUSTOMREQUEST, "PUT"); to PUT data or should use "CURLOPT_UPLOAD". Whats the recommended way of doing it.
I've gone through the CURLOPT_UPLOAD documentation and example code which details how to use CURLOPT_UPLOAD. If I opt to go ahead with this method should I still need to use a file as below
/* now specify which file to upload */
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
/* provide the size of the upload, we specicially typecast the value
to curl_off_t since we must be sure to use the correct data size */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
or simply providing a std::string as below be suffice,
std::string data = "xyz";
curl_easy_setopt(curl, CURLOPT_READDATA, data.c_str());
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)data.length());
Aucun commentaire:
Enregistrer un commentaire