1
/*****************************************************************************
3
* Project ___| | | | _ \| |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
8
* $Id: postit2.c,v 1.1 2003/01/07 02:17:48 andy Exp $
10
* Example code that uploads a file name 'foo' to a remote script that accepts
11
* "HTML form based" (as described in RFC1738) uploads using HTTP POST.
13
* The imaginary form we'll fill in looks like:
15
* <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
16
* Enter file: <input type="file" name="sendfile" size="40">
17
* Enter file name: <input type="text" name="filename" size="30">
18
* <input type="submit" value="send" name="submit">
21
* This exact source code has not been verified to work.
24
/* to make this work under windows, use the win32-functions from the
25
win32socket.c file as well */
30
#include <curl/curl.h>
31
#include <curl/types.h>
32
#include <curl/easy.h>
34
#if LIBCURL_VERSION_NUM < 0x070900
35
#error "curl_formadd() is not introduced until libcurl 7.9 and later"
38
int main(int argc, char *argv[])
43
struct HttpPost *formpost=NULL;
44
struct HttpPost *lastptr=NULL;
45
struct curl_slist *headerlist=NULL;
46
char buf[] = "Expect:";
48
/* Fill in the file upload field */
49
curl_formadd(&formpost,
51
CURLFORM_COPYNAME, "sendfile",
52
CURLFORM_FILE, "postit2.c",
55
/* Fill in the filename field */
56
curl_formadd(&formpost,
58
CURLFORM_COPYNAME, "filename",
59
CURLFORM_COPYCONTENTS, "postit2.c",
63
/* Fill in the submit field too, even if this is rarely needed */
64
curl_formadd(&formpost,
66
CURLFORM_COPYNAME, "submit",
67
CURLFORM_COPYCONTENTS, "send",
70
curl = curl_easy_init();
71
/* initalize custom header list (stating that Expect: 100-continue is not
73
headerlist = curl_slist_append(headerlist, buf);
75
/* what URL that receives this POST */
76
curl_easy_setopt(curl, CURLOPT_URL, "http://curl.haxx.se/examplepost.cgi");
77
if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
78
/* only disable 100-continue header if explicitly requested */
79
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
80
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
81
res = curl_easy_perform(curl);
84
curl_easy_cleanup(curl);
86
/* then cleanup the formpost chain */
87
curl_formfree(formpost);
89
curl_slist_free_all (headerlist);