Prasanth Madhavan
Prasanth Madhavan

Reputation: 13309

Help with Curl ERRORBUFFER

I have a program which uses curl. and a part of it looks like this..

char* Error = NULL;
Error = (char*)malloc(1024);
memset(Error, 0, 1024); 
..............
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, Error);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
    if(CURLE_OK != curl_easy_perform( curl)){
        Error[1023] = '\0';
//      cout << "cURL returned: " <<  Error << endl;
    }
    curl_easy_cleanup(curl);
    free(Error);

as you can see i have used a char * for the error buffer... how to use a string instead of a char * as i already have a std::list<string> that contains other error strings...

and even if there is no error, the cout prints this onto the screen:

cURL returned: Failed writing body (442456 != 998)

What should be the if condition to avoid this?

Upvotes: 0

Views: 4259

Answers (1)

CashCow
CashCow

Reputation: 31445

You cannot use a std::string to receive the error as that does not have a writable buffer.

You can use a std::vector though if you want to use STL and not have to handle memory of arrays.

const size_t errBufSize = 1024;
std::vector<char> errBuf( errBufSize );
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &errBuf[0]); 

You actual error comes probably from the rest of the code, for example if you are passing it an error buffer you probably have to pass it the buffer size too.

I think "Failed writing body" comes from the Curl_write_callback not returning the same number of bytes that were passed to it.

Upvotes: 3

Related Questions