1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
15 #ifdef CURL_DOES_CONVERSIONS
16 /* ASCII representation with escape sequences for non-ASCII platforms */
17 "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
18 "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
19 "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
21 "this is what we post to the silly web server\n";
29 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
31 struct WriteThis *pooh = (struct WriteThis *)userp;
37 *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
38 pooh->readptr++; /* advance pointer */
39 pooh->sizeleft--; /* less data left */
40 return 1; /* we return 1 byte at a time! */
43 return 0; /* no more data left to deliver */
49 CURLcode res=CURLE_OK;
52 struct curl_httppost *formpost=NULL;
53 struct curl_httppost *lastptr=NULL;
54 struct WriteThis pooh;
56 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
57 fprintf(stderr, "curl_global_init() failed\n");
58 return TEST_ERR_MAJOR_BAD;
62 pooh.sizeleft = strlen(data);
64 /* Fill in the file upload field */
65 formrc = curl_formadd(&formpost,
67 CURLFORM_COPYNAME, "sendfile",
68 CURLFORM_STREAM, &pooh,
69 CURLFORM_CONTENTSLENGTH, pooh.sizeleft,
70 CURLFORM_FILENAME, "postit2.c",
74 printf("curl_formadd(1) = %d\n", (int)formrc);
76 /* Fill in the filename field */
77 formrc = curl_formadd(&formpost,
79 CURLFORM_COPYNAME, "filename",
80 #ifdef CURL_DOES_CONVERSIONS
81 /* ASCII representation with escape
82 sequences for non-ASCII platforms */
83 CURLFORM_COPYCONTENTS,
84 "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
86 CURLFORM_COPYCONTENTS, "postit2.c",
91 printf("curl_formadd(2) = %d\n", (int)formrc);
93 /* Fill in a submit field too */
94 formrc = curl_formadd(&formpost,
96 CURLFORM_COPYNAME, "submit",
97 #ifdef CURL_DOES_CONVERSIONS
98 /* ASCII representation with escape
99 sequences for non-ASCII platforms */
100 CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
102 CURLFORM_COPYCONTENTS, "send",
107 printf("curl_formadd(3) = %d\n", (int)formrc);
109 if ((curl = curl_easy_init()) == NULL) {
110 fprintf(stderr, "curl_easy_init() failed\n");
111 curl_formfree(formpost);
112 curl_global_cleanup();
113 return TEST_ERR_MAJOR_BAD;
116 /* First set the URL that is about to receive our POST. */
117 test_setopt(curl, CURLOPT_URL, URL);
119 /* Now specify we want to POST data */
120 test_setopt(curl, CURLOPT_POST, 1L);
122 /* Set the expected POST size */
123 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
125 /* we want to use our own read function */
126 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
128 /* send a multi-part formpost */
129 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
131 /* get verbose debug output please */
132 test_setopt(curl, CURLOPT_VERBOSE, 1L);
134 /* include headers in the output */
135 test_setopt(curl, CURLOPT_HEADER, 1L);
137 /* Perform the request, res will get the return code */
138 res = curl_easy_perform(curl);
143 curl_easy_cleanup(curl);
144 curl_global_cleanup();
146 /* now cleanup the formpost chain */
147 curl_formfree(formpost);