cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: FW: HTTP GET using easycurl - HTTP chunks in HTTP response.

From: John Vorwald <john_vorwald_at_msn.com>
Date: Fri, 15 Dec 2006 06:34:42 -0500

The global decleration of strData can be removed by using the
CURLOPT_WRITEDATA option. The following code snippet illustrates the
statements in the calling function that are used to 1) pass strData to the
write function and 2) specify the name of the write function (write_data).

    // Initalize Curl
    CURL *curl;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    ...

    // Setup void * parameter in write data function
    std::string strData ;
    strData .clear();
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &strData );

    // Set the pointer to the write data function
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, *write_data);
    ...
    res = curl_easy_perform(curl);

In the function write_data, store the HTTP chunks in variable strData with

size_t write_data(void *ptr, size_t size, size_t nmemb, void *strData_ptr)
{
  ((std::string *)strData_ptr)->append((char*)ptr, size*nmemb);
  return (nmemb * size);
}

>From: Daniel Stenberg <daniel_at_haxx.se>
>Reply-To: libcurl development <curl-library_at_cool.haxx.se>
>To: libcurl development <curl-library_at_cool.haxx.se>
>Subject: RE: FW: HTTP GET using easycurl - HTTP chunks in HTTP response.
>Date: Thu, 14 Dec 2006 18:43:44 +0100 (CET)
>
>On Thu, 14 Dec 2006, Rangoli Mathur wrote:
>
>>In write_data function I am just gathering the data, so I can see the
>>response after the perform call returns.
>
>[...]
>
>>size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
>>{
>> strData += ((char*) ptr);
>> return (nmemb * size);
>>}
>
>Allow me to quote the manual:
>
> "The size of the data pointed to by ptr is size multiplied with nmemb, it
> will not be zero terminated."
>
>--
> Commercial curl and libcurl Technical Support: http://haxx.se/curl.html
Received on 2006-12-15