Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL - simple example returning "CURLE_WRITE_ERROR"

Tags:

c++

curl

libcurl

I am trying to run a simple example using libcurl, but just running this simple example gives me CURLE_WRITE_ERROR when I execute the curl_easy_perform(...) command. Does anyone have any idea what I am doing wrong? I have also tried other sites besides example.com.

CURL *curl = curl_easy_init();

if(curl) 
{
    CURLcode res;
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
    res = curl_easy_perform(curl); // returns CURLE_WRITE_ERROR always!
    curl_easy_cleanup(curl);
}
like image 242
eddietree Avatar asked Aug 09 '15 04:08

eddietree


1 Answers

OK turns out Joachim is right. I did need a write callback

size_t CurlWriteCallback(char* buf, size_t size, size_t nmemb, void* up)
{ 
    TRACE("CURL - Response received:\n%s", buf);
    TRACE("CURL - Response handled %d bytes:\n%s", size*nmemb);

    // tell curl how many bytes we handled
    return size*nmemb;
}

// ...

CURL *curl = curl_easy_init();

if(curl) 
{
    CURLcode res;
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &CurlWriteCallback);
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
    res = curl_easy_perform(curl); 
    curl_easy_cleanup(curl);
}
like image 90
eddietree Avatar answered Oct 13 '22 16:10

eddietree