CURLOPT_WRITEFUNCTION.3 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. .\"
  25. .TH CURLOPT_WRITEFUNCTION 3 "February 07, 2023" "libcurl 7.88.1" "curl_easy_setopt options"
  26. .SH NAME
  27. CURLOPT_WRITEFUNCTION \- callback for writing received data
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/curl.h>
  31. size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
  32. CURLcode curl_easy_setopt(CURL *handle, CURLOPT_WRITEFUNCTION, write_callback);
  33. .SH DESCRIPTION
  34. Pass a pointer to your callback function, which should match the prototype
  35. shown above.
  36. This callback function gets called by libcurl as soon as there is data
  37. received that needs to be saved. For most transfers, this callback gets called
  38. many times and each invoke delivers another chunk of data. \fIptr\fP points to
  39. the delivered data, and the size of that data is \fInmemb\fP; \fIsize\fP is
  40. always 1.
  41. The callback function will be passed as much data as possible in all invokes,
  42. but you must not make any assumptions. It may be one byte, it may be
  43. thousands. The maximum amount of body data that will be passed to the write
  44. callback is defined in the curl.h header file: \fICURL_MAX_WRITE_SIZE\fP (the
  45. usual default is 16K). If \fICURLOPT_HEADER(3)\fP is enabled, which makes
  46. header data get passed to the write callback, you can get up to
  47. \fICURL_MAX_HTTP_HEADER\fP bytes of header data passed into it. This usually
  48. means 100K.
  49. This function may be called with zero bytes data if the transferred file is
  50. empty.
  51. The data passed to this function will not be null-terminated!
  52. Set the \fIuserdata\fP argument with the \fICURLOPT_WRITEDATA(3)\fP option.
  53. Your callback should return the number of bytes actually taken care of. If
  54. that amount differs from the amount passed to your callback function, it will
  55. signal an error condition to the library. This will cause the transfer to get
  56. aborted and the libcurl function used will return \fICURLE_WRITE_ERROR\fP.
  57. You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0)
  58. If your callback function returns CURL_WRITEFUNC_PAUSE it will cause this
  59. transfer to become paused. See \fIcurl_easy_pause(3)\fP for further details.
  60. Set this option to NULL to get the internal default function used instead of
  61. your callback. The internal default function will write the data to the FILE *
  62. given with \fICURLOPT_WRITEDATA(3)\fP.
  63. This option does not enable HSTS, you need to use \fICURLOPT_HSTS_CTRL(3)\fP to
  64. do that.
  65. .SH DEFAULT
  66. libcurl will use 'fwrite' as a callback by default.
  67. .SH PROTOCOLS
  68. For all protocols
  69. .SH EXAMPLE
  70. .nf
  71. struct memory {
  72. char *response;
  73. size_t size;
  74. };
  75. static size_t cb(void *data, size_t size, size_t nmemb, void *clientp)
  76. {
  77. size_t realsize = size * nmemb;
  78. struct memory *mem = (struct memory *)clientp;
  79. char *ptr = realloc(mem->response, mem->size + realsize + 1);
  80. if(ptr == NULL)
  81. return 0; /* out of memory! */
  82. mem->response = ptr;
  83. memcpy(&(mem->response[mem->size]), data, realsize);
  84. mem->size += realsize;
  85. mem->response[mem->size] = 0;
  86. return realsize;
  87. }
  88. struct memory chunk = {0};
  89. CURLcode res;
  90. CURL *curl_handle = curl_easy_init();
  91. if (curl_handle)
  92. {
  93. /* send all data to this function */
  94. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, cb);
  95. /* we pass our 'chunk' struct to the callback function */
  96. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  97. /* send a request */
  98. res = curl_easy_perform(curl_handle);
  99. /* remember to free the buffer */
  100. free(chunk.response)
  101. curl_easy_cleanup(curl_handle);
  102. }
  103. .fi
  104. .SH AVAILABILITY
  105. Support for the CURL_WRITEFUNC_PAUSE return code was added in version 7.18.0.
  106. .SH RETURN VALUE
  107. This will return CURLE_OK.
  108. .SH "SEE ALSO"
  109. .BR CURLOPT_WRITEDATA "(3), " CURLOPT_READFUNCTION "(3), "
  110. .BR CURLOPT_HEADERFUNCTION "(3), "