1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib554.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
13 static char data
[]="this is what we post to the silly web server\n";
20 static size_t read_callback(void *ptr
, size_t size
, size_t nmemb
, void *userp
)
22 struct WriteThis
*pooh
= (struct WriteThis
*)userp
;
28 *(char *)ptr
= pooh
->readptr
[0]; /* copy one single byte */
29 pooh
->readptr
++; /* advance pointer */
30 pooh
->sizeleft
--; /* less data left */
31 return 1; /* we return 1 byte at a time! */
34 return 0; /* no more data left to deliver */
40 CURLcode res
=CURLE_OK
;
43 struct curl_httppost
*formpost
=NULL
;
44 struct curl_httppost
*lastptr
=NULL
;
45 struct WriteThis pooh
;
47 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
) {
48 fprintf(stderr
, "curl_global_init() failed\n");
49 return TEST_ERR_MAJOR_BAD
;
53 pooh
.sizeleft
= strlen(data
);
55 /* Fill in the file upload field */
56 formrc
= curl_formadd(&formpost
,
58 CURLFORM_COPYNAME
, "sendfile",
59 CURLFORM_STREAM
, &pooh
,
60 CURLFORM_CONTENTSLENGTH
, pooh
.sizeleft
,
61 CURLFORM_FILENAME
, "postit2.c",
65 printf("curl_formadd(1) = %d\n", (int)formrc
);
67 /* Fill in the filename field */
68 formrc
= curl_formadd(&formpost
,
70 CURLFORM_COPYNAME
, "filename",
71 CURLFORM_COPYCONTENTS
, "postit2.c",
75 printf("curl_formadd(2) = %d\n", (int)formrc
);
77 /* Fill in a submit field too */
78 formrc
= curl_formadd(&formpost
,
80 CURLFORM_COPYNAME
, "submit",
81 CURLFORM_COPYCONTENTS
, "send",
85 printf("curl_formadd(3) = %d\n", (int)formrc
);
87 if ((curl
= curl_easy_init()) == NULL
) {
88 fprintf(stderr
, "curl_easy_init() failed\n");
89 curl_formfree(formpost
);
90 curl_global_cleanup();
91 return TEST_ERR_MAJOR_BAD
;
94 /* First set the URL that is about to receive our POST. */
95 curl_easy_setopt(curl
, CURLOPT_URL
, URL
);
97 /* Now specify we want to POST data */
98 curl_easy_setopt(curl
, CURLOPT_POST
, 1L);
100 /* Set the expected POST size */
101 curl_easy_setopt(curl
, CURLOPT_POSTFIELDSIZE
, (long)pooh
.sizeleft
);
103 /* we want to use our own read function */
104 curl_easy_setopt(curl
, CURLOPT_READFUNCTION
, read_callback
);
106 /* pointer to pass to our read function */
107 curl_easy_setopt(curl
, CURLOPT_READDATA
, &pooh
);
109 /* send a multi-part formpost */
110 curl_easy_setopt(curl
, CURLOPT_HTTPPOST
, formpost
);
112 /* get verbose debug output please */
113 curl_easy_setopt(curl
, CURLOPT_VERBOSE
, 1L);
115 /* include headers in the output */
116 curl_easy_setopt(curl
, CURLOPT_HEADER
, 1L);
118 /* Perform the request, res will get the return code */
119 res
= curl_easy_perform(curl
);
122 curl_easy_cleanup(curl
);
123 curl_global_cleanup();
125 /* now cleanup the formpost chain */
126 curl_formfree(formpost
);