CURLOPT_WRITEFUNCTION.3 3.7 KB

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