From 56e8d073bf26231d84f7ea9e8c5102966dfa4b2c Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 23 Aug 2001 08:45:20 +0000 Subject: curl_formadd() using example, the 7.9 style of building rfc1867 form posts --- docs/examples/postit2.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 docs/examples/postit2.c (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c new file mode 100644 index 000000000..9b7cda07e --- /dev/null +++ b/docs/examples/postit2.c @@ -0,0 +1,92 @@ +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + * $Id$ + * + * Example code that uploads a file name 'foo' to a remote script that accepts + * "HTML form based" (as described in RFC1738) uploads using HTTP POST. + * + * The imaginary form we'll fill in looks like: + * + *
+ * Enter file: + * Enter file name: + * + *
+ * + * This exact source code has not been verified to work. + */ + +/* to make this work under windows, use the win32-functions from the + win32socket.c file as well */ + +#include +#include + +#include +#include +#include + +#if LIBCURL_VERSION_NUM < 0x070900 +#error "curl_formadd() is not introduced until libcurl 7.9 and later" +#endif + +int main(int argc, char *argv[]) +{ + CURL *curl; + CURLcode res; + + struct HttpPost *formpost=NULL; + struct HttpPost *lastptr=NULL; + struct curl_slist *headerlist=NULL; + char buf[] = "Expect:"; + + /* Fill in the file upload field */ + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "sendfile", + CURLFORM_FILE, "postit2.c", + CURLFORM_END); + + /* Fill in the filename field */ + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "filename", + CURLFORM_COPYCONTENTS, "postit2.c", + CURLFORM_END); + + + /* Fill in the submit field too, even if this is rarely needed */ + curl_formadd(&formpost, + &lastptr, + CURLFORM_COPYNAME, "submit", + CURLFORM_COPYCONTENTS, "send", + CURLFORM_END); + + curl = curl_easy_init(); + /* initalize custom header list (stating that Expect: 100-continue is not + wanted */ + headerlist = curl_slist_append(headerlist, buf); + if(curl) { + /* what URL that receives this POST */ + curl_easy_setopt(curl, CURLOPT_URL, "http://curl.haxx.se/examplepost.cgi"); + if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) ) + /* only disable 100-continue header if explicitly requested */ + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); + curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + res = curl_easy_perform(curl); + + /* always cleanup */ + curl_easy_cleanup(curl); + + /* then cleanup the formpost chain */ + curl_formfree(formpost); + /* free slist */ + curl_slist_free_all (headerlist); + } + return 0; +} -- cgit v1.2.1 From cc48658564dad6cd57b7526ab1e98780145a473f Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 19 Nov 2003 08:21:34 +0000 Subject: cut off old crappy win32 comments and use the proper global_init instead also removed very old "require libcurl older than blablabla" --- docs/examples/postit2.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index 9b7cda07e..a46f0a71b 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -21,9 +21,6 @@ * This exact source code has not been verified to work. */ -/* to make this work under windows, use the win32-functions from the - win32socket.c file as well */ - #include #include @@ -31,10 +28,6 @@ #include #include -#if LIBCURL_VERSION_NUM < 0x070900 -#error "curl_formadd() is not introduced until libcurl 7.9 and later" -#endif - int main(int argc, char *argv[]) { CURL *curl; @@ -45,6 +38,8 @@ int main(int argc, char *argv[]) struct curl_slist *headerlist=NULL; char buf[] = "Expect:"; + curl_global_init(CURL_GLOBAL_ALL); + /* Fill in the file upload field */ curl_formadd(&formpost, &lastptr, -- cgit v1.2.1 From 6b33a5f954c11d77c82ead15e3e5560ca8568c42 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Sun, 28 Mar 2004 21:41:10 +0000 Subject: use the correct struct --- docs/examples/postit2.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index a46f0a71b..d1ecadd61 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -33,8 +33,8 @@ int main(int argc, char *argv[]) CURL *curl; CURLcode res; - struct HttpPost *formpost=NULL; - struct HttpPost *lastptr=NULL; + struct curl_httppost *formpost=NULL; + struct curl_httppost *lastptr=NULL; struct curl_slist *headerlist=NULL; char buf[] = "Expect:"; -- cgit v1.2.1 From d736ac51c047119c8d82df9f18b127cbcecc86c6 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 23 Aug 2004 14:22:52 +0000 Subject: stripped trailing whitespace --- docs/examples/postit2.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index d1ecadd61..e022d284f 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -1,8 +1,8 @@ /***************************************************************************** - * _ _ ____ _ - * Project ___| | | | _ \| | - * / __| | | | |_) | | - * | (__| |_| | _ <| |___ + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * * $Id$ @@ -53,7 +53,7 @@ int main(int argc, char *argv[]) CURLFORM_COPYNAME, "filename", CURLFORM_COPYCONTENTS, "postit2.c", CURLFORM_END); - + /* Fill in the submit field too, even if this is rarely needed */ curl_formadd(&formpost, -- cgit v1.2.1 From 49ce3e5160a9576e797bf87cef012b09d1c54ecb Mon Sep 17 00:00:00 2001 From: Dan Fandrich Date: Thu, 12 Jul 2007 21:11:10 +0000 Subject: Fixed some compile warnings and errors and improved portability in the examples. Removed ftp3rdparty.c since libcurl doesn't support 3rd party FTP transfers any longer. --- docs/examples/postit2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index e022d284f..0660cc5b5 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -36,7 +36,7 @@ int main(int argc, char *argv[]) struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; struct curl_slist *headerlist=NULL; - char buf[] = "Expect:"; + static const char buf[] = "Expect:"; curl_global_init(CURL_GLOBAL_ALL); -- cgit v1.2.1 From 2309b4e330b96bc2e1f8e36b6184015e59544037 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Wed, 24 Mar 2010 11:02:54 +0100 Subject: remove the CVSish $Id$ lines --- docs/examples/postit2.c | 1 - 1 file changed, 1 deletion(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index 0660cc5b5..7268fcbbc 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -5,7 +5,6 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * $Id$ * * Example code that uploads a file name 'foo' to a remote script that accepts * "HTML form based" (as described in RFC1738) uploads using HTTP POST. -- cgit v1.2.1 From 18e7b52e8e95f7204d8ef7ff0f0ff0043afe45a9 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 5 Oct 2010 15:00:19 +0200 Subject: examples: use example.com in example URLs --- docs/examples/postit2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index 7268fcbbc..a6292c5f1 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -67,7 +67,7 @@ int main(int argc, char *argv[]) headerlist = curl_slist_append(headerlist, buf); if(curl) { /* what URL that receives this POST */ - curl_easy_setopt(curl, CURLOPT_URL, "http://curl.haxx.se/examplepost.cgi"); + curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi"); if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) ) /* only disable 100-continue header if explicitly requested */ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); -- cgit v1.2.1 From 1aeb635cdd296c16acb375a4a83a78f13166ccab Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 10 Mar 2011 11:48:02 +0100 Subject: sources: update source headers All C and H files now (should) feature the proper project curl source code header, which includes basic info, a copyright statement and some basic disclaimers. --- docs/examples/postit2.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index a6292c5f1..bb7fd48d7 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -1,12 +1,25 @@ -/***************************************************************************** +/*************************************************************************** * _ _ ____ _ * Project ___| | | | _ \| | * / __| | | | |_) | | * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * + * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. * - * Example code that uploads a file name 'foo' to a remote script that accepts + * This software is licensed as described in the file COPYING, which + * you should have received as part of this distribution. The terms + * are also available at http://curl.haxx.se/docs/copyright.html. + * + * You may opt to use, copy, modify, merge, publish, distribute and/or sell + * copies of the Software, and permit persons to whom the Software is + * furnished to do so, under the terms of the COPYING file. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ***************************************************************************/ +/* Example code that uploads a file name 'foo' to a remote script that accepts * "HTML form based" (as described in RFC1738) uploads using HTTP POST. * * The imaginary form we'll fill in looks like: -- cgit v1.2.1 From ac28971aa61d28e5dd54888e34e958d1c742b461 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 28 Jun 2011 19:08:51 +0200 Subject: examples: cleanup curl includes Only is needed typically and curl/types.h has been removed --- docs/examples/postit2.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index bb7fd48d7..63c248467 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -37,8 +37,6 @@ #include #include -#include -#include int main(int argc, char *argv[]) { -- cgit v1.2.1 From a3dbbcfd2ac7ab8f597e26e76935726279d3fa0c Mon Sep 17 00:00:00 2001 From: Guenter Knauf Date: Wed, 4 Jul 2012 17:03:52 +0200 Subject: Added error checking for samples. --- docs/examples/postit2.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'docs/examples/postit2.c') diff --git a/docs/examples/postit2.c b/docs/examples/postit2.c index 63c248467..67dcc1330 100644 --- a/docs/examples/postit2.c +++ b/docs/examples/postit2.c @@ -83,7 +83,13 @@ int main(int argc, char *argv[]) /* only disable 100-continue header if explicitly requested */ curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); + + /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); + /* Check for errors */ + if(res != CURLE_OK) + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); /* always cleanup */ curl_easy_cleanup(curl); -- cgit v1.2.1