postit2-formadd.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* <DESC>
  23. * HTTP Multipart formpost with file upload and two additional parts.
  24. * </DESC>
  25. */
  26. /* Example code that uploads a file name 'foo' to a remote script that accepts
  27. * "HTML form based" (as described in RFC1738) uploads using HTTP POST.
  28. *
  29. * The imaginary form we'll fill in looks like:
  30. *
  31. * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
  32. * Enter file: <input type="file" name="sendfile" size="40">
  33. * Enter file name: <input type="text" name="filename" size="30">
  34. * <input type="submit" value="send" name="submit">
  35. * </form>
  36. *
  37. * This exact source code has not been verified to work.
  38. */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <curl/curl.h>
  42. int main(int argc, char *argv[])
  43. {
  44. CURL *curl;
  45. CURLcode res;
  46. struct curl_httppost *formpost = NULL;
  47. struct curl_httppost *lastptr = NULL;
  48. struct curl_slist *headerlist = NULL;
  49. static const char buf[] = "Expect:";
  50. curl_global_init(CURL_GLOBAL_ALL);
  51. /* Fill in the file upload field */
  52. curl_formadd(&formpost,
  53. &lastptr,
  54. CURLFORM_COPYNAME, "sendfile",
  55. CURLFORM_FILE, "postit2.c",
  56. CURLFORM_END);
  57. /* Fill in the filename field */
  58. curl_formadd(&formpost,
  59. &lastptr,
  60. CURLFORM_COPYNAME, "filename",
  61. CURLFORM_COPYCONTENTS, "postit2.c",
  62. CURLFORM_END);
  63. /* Fill in the submit field too, even if this is rarely needed */
  64. curl_formadd(&formpost,
  65. &lastptr,
  66. CURLFORM_COPYNAME, "submit",
  67. CURLFORM_COPYCONTENTS, "send",
  68. CURLFORM_END);
  69. curl = curl_easy_init();
  70. /* initialize custom header list (stating that Expect: 100-continue is not
  71. wanted */
  72. headerlist = curl_slist_append(headerlist, buf);
  73. if(curl) {
  74. /* what URL that receives this POST */
  75. curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/examplepost.cgi");
  76. if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
  77. /* only disable 100-continue header if explicitly requested */
  78. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  79. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  80. /* Perform the request, res will get the return code */
  81. res = curl_easy_perform(curl);
  82. /* Check for errors */
  83. if(res != CURLE_OK)
  84. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  85. curl_easy_strerror(res));
  86. /* always cleanup */
  87. curl_easy_cleanup(curl);
  88. /* then cleanup the formpost chain */
  89. curl_formfree(formpost);
  90. /* free slist */
  91. curl_slist_free_all(headerlist);
  92. }
  93. return 0;
  94. }