1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: post-callback.c,v 1.3 2003/12/08 14:14:26 bagder Exp $
10 * An example source code that issues a HTTP POST and we provide the actual
11 * data through a read callback.
16 #include <curl/curl.h>
18 char data[]="this is what we post to the silly web server";
25 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
27 struct WriteThis *pooh = (struct WriteThis *)userp;
33 *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
34 pooh->readptr++; /* advance pointer */
35 pooh->sizeleft--; /* less data left */
36 return 1; /* we return 1 byte at a time! */
39 return -1; /* no more data left to deliver */
47 struct WriteThis pooh;
50 pooh.sizeleft = strlen(data);
52 curl = curl_easy_init();
54 /* First set the URL that is about to receive our POST. */
55 curl_easy_setopt(curl, CURLOPT_URL,
56 "http://receivingsite.com.pooh/index.cgi");
57 /* Now specify we want to POST data */
58 curl_easy_setopt(curl, CURLOPT_POST, TRUE);
60 /* Set the expected POST size */
61 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
63 /* we want to use our own read function */
64 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
66 /* pointer to pass to our read function */
67 curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
69 /* get verbose debug output please */
70 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
72 /* Perform the request, res will get the return code */
73 res = curl_easy_perform(curl);
76 curl_easy_cleanup(curl);