Reputation: 45
Hi I'm trying to save CURL server response and getting following error: I read that is due to difference in size between data read and written to a file so curl is crushing. File is created and contains need XML data but still shows error. THANK you for your help!!
Failed writing body (0 != 96)
Failed writing data
Closing connection #0
here are my cb function:
static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{ size_t written = fwrite(ptr, size, nmemb, stream); return written; }
and method:
createJob(std::string s, std::string directory) {
static const char *fp = "resultcurl.xml";
std::stringstream filepath;
filepath<< directory;
filepath<< fp;
std::string pagefilename = filepath.str();
FILE *pagefile;
std::string job_id;
struct curl_slist *headers = NULL;
std::string jobXML = s;
curl = curl_easy_init();
if (curl) {
headers = curl_slist_append(headers, "Accept: */*");
headers = curl_slist_append(headers, "Content-Type: application/xml");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_VERBOSE, true);
curl_easy_setopt(curl, CURLOPT_URL,
".../jobs.xml");
curl_easy_setopt(curl, CURLOPT_USERPWD, "...");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jobXML.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(jobXML.c_str()));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,write_data);
pagefile = fopen(pagefilename.c_str(), "wb");
if (pagefile) {
curl_easy_setopt(curl, CURLOPT_FILE, pagefile);
//curl_easy_perform(curl);
}
res = curl_easy_perform(curl);
/* cleanup curl stuff */
curl_easy_cleanup(curl);
//job_id =readXMLvalue(pagefilename);
fclose(pagefile);
return 0;
}
Upvotes: 0
Views: 1762
Reputation: 1009
If you just want to write data into the file, Curl will do it for you. set
if(curl_easy_setopt( curl , CURLOPT_WRITEDATA , (void *) fptr ) != CURLE_OK)
{
return(YOUR_ERR_CODE) ;
}
where fptr is,
FILE *fptr = fopen( "emalware.127.5a6ceb78050f80e4c833dcc3764bf9dd.gzip", "wb" );
if( fptr == NULL )
{
return -1;
}
Upvotes: 0
Reputation: 14880
Swap the order of the setup,
pagefile = fopen(pagefilename.c_str(), "wb");
if (pagefile) {
curl_easy_setopt(curl, CURLOPT_FILE, pagefile);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,write_data);
...
}
...
Upvotes: 2