2
0

postit2-formadd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* <DESC>
  25. * HTTP Multipart formpost with file upload and two additional parts.
  26. * </DESC>
  27. */
  28. /*
  29. * Example code that uploads a filename 'foo' to a remote script that accepts
  30. * "HTML form based" (as described in RFC 1738) uploads using HTTP POST.
  31. *
  32. * Warning: this example uses the deprecated form api. See "postit2.c"
  33. * for a similar example using the mime api.
  34. *
  35. * The imaginary form we fill in looks like:
  36. *
  37. * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
  38. * Enter file: <input type="file" name="sendfile" size="40">
  39. * Enter filename: <input type="text" name="filename" size="30">
  40. * <input type="submit" value="send" name="submit">
  41. * </form>
  42. */
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <curl/curl.h>
  46. #ifdef __GNUC__
  47. #pragma GCC diagnostic push
  48. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  49. #endif
  50. int main(int argc, char *argv[])
  51. {
  52. CURL *curl;
  53. CURLcode res;
  54. struct curl_httppost *formpost = NULL;
  55. struct curl_httppost *lastptr = NULL;
  56. struct curl_slist *headerlist = NULL;
  57. static const char buf[] = "Expect:";
  58. curl_global_init(CURL_GLOBAL_ALL);
  59. /* Fill in the file upload field */
  60. curl_formadd(&formpost,
  61. &lastptr,
  62. CURLFORM_COPYNAME, "sendfile",
  63. CURLFORM_FILE, "postit2-formadd.c",
  64. CURLFORM_END);
  65. /* Fill in the filename field */
  66. curl_formadd(&formpost,
  67. &lastptr,
  68. CURLFORM_COPYNAME, "filename",
  69. CURLFORM_COPYCONTENTS, "postit2-formadd.c",
  70. CURLFORM_END);
  71. /* Fill in the submit field too, even if this is rarely needed */
  72. curl_formadd(&formpost,
  73. &lastptr,
  74. CURLFORM_COPYNAME, "submit",
  75. CURLFORM_COPYCONTENTS, "send",
  76. CURLFORM_END);
  77. curl = curl_easy_init();
  78. /* initialize custom header list (stating that Expect: 100-continue is not
  79. wanted */
  80. headerlist = curl_slist_append(headerlist, buf);
  81. if(curl) {
  82. /* what URL that receives this POST */
  83. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/examplepost.cgi");
  84. if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
  85. /* only disable 100-continue header if explicitly requested */
  86. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  87. curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
  88. /* Perform the request, res gets the return code */
  89. res = curl_easy_perform(curl);
  90. /* Check for errors */
  91. if(res != CURLE_OK)
  92. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  93. curl_easy_strerror(res));
  94. /* always cleanup */
  95. curl_easy_cleanup(curl);
  96. /* then cleanup the formpost chain */
  97. curl_formfree(formpost);
  98. /* free slist */
  99. curl_slist_free_all(headerlist);
  100. }
  101. return 0;
  102. }
  103. #ifdef __GNUC__
  104. #pragma GCC diagnostic pop
  105. #endif