milo
milo

Reputation: 1220

receiving data after curl_easy_perform

I have the following question: how can i write data returning with http-response in char * buffer? I've found several approaches:

  1. use CURLOPT_WRITEDATA or CURLOPT_WRITEFUNCTION. but CURLOPT_WRITEDATA requires file pointer (FILE *). use of CURLOPT_WRITEFUNCTION with callback function seems to me as quirk...
  2. use curl_easy_send and curl_easy_recv. but in this case i'll need to write all POST headers with hands...

Is there some other, more elegant approach? e.g. pass char * buffer pointer into some function to get http response in.

Upvotes: 1

Views: 2483

Answers (1)

Missaka Wijekoon
Missaka Wijekoon

Reputation: 891

Actually CURLOPT_WRITEDATA and CURLOPT_WRITEFUNCTION can be used with any pointer type. As long as your function is compatible with that pointer type.

For example:

    ...
    client_t *client;
    CURL *conn;
    ...
    curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, read_data);
    curl_easy_setopt(conn, CURLOPT_WRITEDATA, client);
    ...

static size_t read_data(void *ptr,
                        size_t size,
                        size_t nmemb,
                        client_t *client)
{
     memcpy(client->data, ptr, size * nmemb);
     return size * nmemb;
}

Upvotes: 2

Related Questions