cURL / Mailing Lists / curl-library / Single Mail


Re: Libcurl newbie question

From: <>
Date: Thu, 6 Apr 2006 15:53:19 -0000

On Thu, Apr 6, 2006, Saikat Kanjilal <> said:

> Hi All:
> I have a client side app that does a simple http multipart post and sends a
> file upto a server. I was wondering what the options are in libcurl to read
> the response sent back by the server. The server is simply going to send
> back a string, also some pros and cons of each option would be immensely
> valuable to me.

i have a program that does this exact thing, here is what i use:

#define MAX_RESP_LEN 512
struct resp_struct {
    int used;
    int len;
    char bytes[MAX_RESP_LEN];

httpWriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
  int realsize = size * nmemb;
  struct resp_struct *resp = (struct resp_struct *)data;

    resp->used = (realsize < resp->len) ? realsize : resp->len;
    memcpy(&resp->bytes, ptr, resp->used);

  return realsize;

in main()

  struct resp_struct resp = {0,MAX_RESP_LEN,{0}};

  if(curl_easy_setopt(mycurl, CURLOPT_WRITEFUNCTION, httpWriteCallback)){
    fprintf(stderr,"CHILD: Could not add write callback: ERROR\n");
    fprintf(stderr,"CHILD: %s\n",(char*)&error_buffer);

  if(curl_easy_setopt(mycurl, CURLOPT_WRITEDATA, (void *)&resp)){
    fprintf(stderr,"CHILD: Could not add write pointer: ERROR\n");
    fprintf(stderr,"CHILD: %s\n",(char*)&error_buffer);

libcurl will call the callback multiple times, if the http response
requires it. my code only buffers the first call to the callback, and then
only the first 512 bytes of that call. after that it 'lies' to libcurl,
and says it did something with the data. that is enough for my use, as my
handler on the server side sends back a two word text response, and i have
never seen libcurl break that up over two calls to the callback. if that
ever happens, i am in trouble :)



m. allan noah
IT Director,
ph# (804) 355-5489
tf# (866) 724-9722
fx# (804) 355-0477
Received on 2006-04-06