]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/curl/docs/examples/post-callback.c
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / curl / docs / examples / post-callback.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id: post-callback.c,v 1.3 2003/12/08 14:14:26 bagder Exp $
9  *
10  * An example source code that issues a HTTP POST and we provide the actual
11  * data through a read callback.
12  *
13  */
14 #include <stdio.h>
15 #include <string.h>
16 #include <curl/curl.h>
17
18 char data[]="this is what we post to the silly web server";
19
20 struct WriteThis {
21   char *readptr;
22   int sizeleft;
23 };
24
25 size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
26 {
27   struct WriteThis *pooh = (struct WriteThis *)userp;
28
29   if(size*nmemb < 1)
30     return 0;
31
32   if(pooh->sizeleft) {
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! */
37   }
38
39   return -1;                         /* no more data left to deliver */
40 }
41
42 int main(void)
43 {
44   CURL *curl;
45   CURLcode res;
46
47   struct WriteThis pooh;
48
49   pooh.readptr = data;
50   pooh.sizeleft = strlen(data);
51
52   curl = curl_easy_init();
53   if(curl) {
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);
59
60     /* Set the expected POST size */
61     curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, pooh.sizeleft);
62
63     /* we want to use our own read function */
64     curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
65
66     /* pointer to pass to our read function */
67     curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
68
69     /* get verbose debug output please */
70     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
71
72     /* Perform the request, res will get the return code */
73     res = curl_easy_perform(curl);
74
75     /* always cleanup */
76     curl_easy_cleanup(curl);
77   }
78   return 0;
79 }