postit2.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  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.
  12. *
  13. * The imaginary form we'll fill in looks like:
  14. *
  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">
  19. * </form>
  20. *
  21. * This exact source code has not been verified to work.
  22. */
  23. /* to make this work under windows, use the win32-functions from the
  24. win32socket.c file as well */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <curl/curl.h>
  28. #include <curl/types.h>
  29. #include <curl/easy.h>
  30. #if LIBCURL_VERSION_NUM < 0x070900
  31. #error "curl_formadd() is not introduced until libcurl 7.9 and later"
  32. #endif
  33. int main(int argc, char *argv[])
  34. {
  35. CURL *curl;
  36. CURLcode res;
  37. struct HttpPost *formpost=NULL;
  38. struct HttpPost *lastptr=NULL;
  39. struct curl_slist *headerlist=NULL;
  40. char buf[] = "Expect:";
  41. /* Fill in the file upload field */
  42. curl_formadd(&formpost,
  43. &lastptr,
  44. CURLFORM_COPYNAME, "sendfile",
  45. CURLFORM_FILE, "postit2.c",
  46. CURLFORM_END);
  47. /* Fill in the filename field */
  48. curl_formadd(&formpost,
  49. &lastptr,
  50. CURLFORM_COPYNAME, "filename",
  51. CURLFORM_COPYCONTENTS, "postit2.c",
  52. CURLFORM_END);
  53. /* Fill in the submit field too, even if this is rarely needed */
  54. curl_formadd(&formpost,
  55. &lastptr,
  56. CURLFORM_COPYNAME, "submit",
  57. CURLFORM_COPYCONTENTS, "send",
  58. CURLFORM_END);
  59. curl = curl_easy_init();
  60. /* initalize custom header list (stating that Expect: 100-continue is not
  61. wanted */
  62. headerlist = curl_slist_append(headerlist, buf);
  63. if(curl) {
  64. /* what URL that receives this POST */
  65. curl_easy_setopt(curl, CURLOPT_URL, "http://curl.haxx.se/examplepost.cgi");
  66. if ( (argc == 2) && (!strcmp(argv[1], "noexpectheader")) )
  67. /* only disable 100-continue header if explicitly requested */
  68. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  69. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  70. res = curl_easy_perform(curl);
  71. /* always cleanup */
  72. curl_easy_cleanup(curl);
  73. /* then cleanup the formpost chain */
  74. curl_formfree(formpost);
  75. /* free slist */
  76. curl_slist_free_all (headerlist);
  77. }
  78. return 0;
  79. }