Search…
Non-blocking HTTP form-post
This examples makes a multipart form-post using the multi interface.
1
#include <stdio.h>
2
#include <string.h>
3
#include <sys/time.h>
4
5
#include <curl/curl.h>
6
7
int main(void)
8
{
9
CURL *curl;
10
11
CURLM *multi_handle;
12
int still_running = 0;
13
14
curl_mime *form = NULL;
15
curl_mimepart *field = NULL;
16
struct curl_slist *headerlist = NULL;
17
static const char buf[] = "Expect:";
18
19
curl = curl_easy_init();
20
multi_handle = curl_multi_init();
21
22
if(curl && multi_handle) {
23
/* Create the form */
24
form = curl_mime_init(curl);
25
26
/* Fill in the file upload field */
27
field = curl_mime_addpart(form);
28
curl_mime_name(field, "sendfile");
29
curl_mime_filedata(field, "multi-post.c");
30
31
/* Fill in the filename field */
32
field = curl_mime_addpart(form);
33
curl_mime_name(field, "filename");
34
curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED);
35
36
/* Fill in the submit field too, even if this is rarely needed */
37
field = curl_mime_addpart(form);
38
curl_mime_name(field, "submit");
39
curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
40
41
/* initialize custom header list (stating that Expect: 100-continue is not
42
wanted */
43
headerlist = curl_slist_append(headerlist, buf);
44
45
/* what URL that receives this POST */
46
curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi");
47
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
48
49
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
50
curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
51
52
curl_multi_add_handle(multi_handle, curl);
53
54
do {
55
CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
56
57
if(still_running)
58
/* wait for activity, timeout or "nothing" */
59
mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
60
61
if(mc)
62
break;
63
} while(still_running);
64
65
curl_multi_cleanup(multi_handle);
66
67
/* always cleanup */
68
curl_easy_cleanup(curl);
69
70
/* then cleanup the form */
71
curl_mime_free(form);
72
73
/* free slist */
74
curl_slist_free_all(headerlist);
75
}
76
return 0;
77
}
Copied!
Export as PDF
Copy link
Edit on GitHub