Pingu
Pingu

Reputation: 646

C++ CURL: treating header and body data differently

I wish to write a C++ program to save the returned header to a variable and save the returned body to a text file. How can I do this?

Currently, my approach is to overload the handleData function, but the compiler returns the error overloaded function with no contextual type information. This is what I have written so far (an extract of the code):

static size_t handleData(char *ptr, size_t size, size_t nmemb, string *str){ 
    string temp = string(ptr);
    // catch the cookie 
    if (temp.substr(0,10)=="Set-Cookie"){
       *str = temp;
    }
    return size * nmemb;
}

static size_t handleData(char *ptr, size_t size, size_t nmemb, FILE *stream){ 
    int written = fwrite(ptr, size, nmemb, stream);
    return written;
}

FILE *bodyfile;
string *return_header = new string;

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleData); 
curl_easy_setopt(curl, CURLOPT_HEADERDATA, return_header);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile);

Upvotes: 1

Views: 1619

Answers (1)

timrau
timrau

Reputation: 23058

You should use CURLOPT_HEADERFUNCTION instead.

static size_t handleHeader(char *ptr, size_t size, size_t nmemb, string *str){ 
    // ...
}
static size_t handleData(char *ptr, size_t size, size_t nmemb, FILE *stream){ 
    // ...
}

curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, handleHeader);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleData); 
curl_easy_setopt(curl, CURLOPT_HEADERDATA, return_header);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile);

Upvotes: 2

Related Questions