postit2.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /* Example code that uploads a filename 'foo' to a remote script that accepts
  29. * "HTML form based" (as described in RFC 1738) uploads using HTTP POST.
  30. *
  31. * The imaginary form we fill in looks like:
  32. *
  33. * <form method="post" enctype="multipart/form-data" action="examplepost.cgi">
  34. * Enter file: <input type="file" name="sendfile" size="40">
  35. * Enter filename: <input type="text" name="filename" size="30">
  36. * <input type="submit" value="send" name="submit">
  37. * </form>
  38. *
  39. */
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <curl/curl.h>
  43. int main(int argc, char *argv[])
  44. {
  45. CURL *curl;
  46. CURLcode res;
  47. curl_mime *form = NULL;
  48. curl_mimepart *field = NULL;
  49. struct curl_slist *headerlist = NULL;
  50. static const char buf[] = "Expect:";
  51. curl_global_init(CURL_GLOBAL_ALL);
  52. curl = curl_easy_init();
  53. if(curl) {
  54. /* Create the form */
  55. form = curl_mime_init(curl);
  56. /* Fill in the file upload field */
  57. field = curl_mime_addpart(form);
  58. curl_mime_name(field, "sendfile");
  59. curl_mime_filedata(field, "postit2.c");
  60. /* Fill in the filename field */
  61. field = curl_mime_addpart(form);
  62. curl_mime_name(field, "filename");
  63. curl_mime_data(field, "postit2.c", CURL_ZERO_TERMINATED);
  64. /* Fill in the submit field too, even if this is rarely needed */
  65. field = curl_mime_addpart(form);
  66. curl_mime_name(field, "submit");
  67. curl_mime_data(field, "send", CURL_ZERO_TERMINATED);
  68. /* initialize custom header list (stating that Expect: 100-continue is not
  69. wanted */
  70. headerlist = curl_slist_append(headerlist, buf);
  71. /* what URL that receives this POST */
  72. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/examplepost.cgi");
  73. if((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
  74. /* only disable 100-continue header if explicitly requested */
  75. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
  76. curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
  77. /* Perform the request, res gets the return code */
  78. res = curl_easy_perform(curl);
  79. /* Check for errors */
  80. if(res != CURLE_OK)
  81. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  82. curl_easy_strerror(res));
  83. /* always cleanup */
  84. curl_easy_cleanup(curl);
  85. /* then cleanup the form */
  86. curl_mime_free(form);
  87. /* free slist */
  88. curl_slist_free_all(headerlist);
  89. }
  90. return 0;
  91. }