1CURLOPT_HEADERFUNCTION(3)           libcurl          CURLOPT_HEADERFUNCTION(3)
2
3
4

NAME

6       CURLOPT_HEADERFUNCTION - callback that receives header data
7

SYNOPSIS

9       #include <curl/curl.h>
10
11       size_t header_callback(char *buffer,
12                              size_t size,
13                              size_t nitems,
14                              void *userdata);
15
16       CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION,
17                                 header_callback);
18

DESCRIPTION

20       Pass a pointer to your callback function, which should match the proto‐
21       type shown above.
22
23       This function gets called by libcurl as soon as it has received  header
24       data.  The header callback will be called once for each header and only
25       complete header lines are passed on to the callback. Parsing headers is
26       easy  to  do  using this callback. buffer points to the delivered data,
27       and the size of that data is nitems; size is always 1.  Do  not  assume
28       that the header line is null-terminated!
29
30       The  pointer named userdata is the one you set with the CURLOPT_HEADER‐
31       DATA(3) option.
32
33       Your callback should return the number of bytes actually taken care of.
34       If  that  amount  differs from the amount passed to your callback func‐
35       tion, it will signal an error condition to the library. This will cause
36       the  transfer  to get aborted and the libcurl function used will return
37       CURLE_WRITE_ERROR.
38
39       You can also abort  the  transfer  by  returning  CURL_WRITEFUNC_ERROR.
40       (7.87.0)
41
42       A  complete  HTTP  header  that is passed to this function can be up to
43       CURL_MAX_HTTP_HEADER (100K) bytes and includes the final line  termina‐
44       tor.
45
46       If this option is not set, or if it is set to NULL, but CURLOPT_HEADER‐
47       DATA(3) is set to anything but NULL, the function used  to  accept  re‐
48       sponse  data  will  be  used  instead. That is, it will be the function
49       specified with CURLOPT_WRITEFUNCTION(3), or if it is not  specified  or
50       NULL - the default, stream-writing function.
51
52       It's  important to note that the callback will be invoked for the head‐
53       ers of all responses received after initiating a request and  not  just
54       the  final response. This includes all responses which occur during au‐
55       thentication negotiation. If you need to operate on  only  the  headers
56       from  the final response, you will need to collect headers in the call‐
57       back yourself and use HTTP status lines, for example,  to  delimit  re‐
58       sponse boundaries.
59
60       For  an HTTP transfer, the status line and the blank line preceding the
61       response body are both included as headers and passed to this function.
62
63       When a server sends a  chunked  encoded  transfer,  it  may  contain  a
64       trailer.  That  trailer  is  identical  to an HTTP header and if such a
65       trailer is received it is passed to the application using this callback
66       as well. There are several ways to detect it being a trailer and not an
67       ordinary header: 1) it comes after the response-body. 2) it comes after
68       the  final  header  line (CR LF) 3) a Trailer: header among the regular
69       response-headers mention what header(s) to expect in the trailer.
70
71       For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function will
72       get  called  with  the  server  responses  to the commands that libcurl
73       sends.
74

LIMITATIONS

76       libcurl does not unfold HTTP "folded  headers"  (deprecated  since  RFC
77       7230).  A folded header is a header that continues on a subsequent line
78       and starts with a whitespace. Such folds will be passed to  the  header
79       callback as a separate one, although strictly it is just a continuation
80       of the previous line.
81

DEFAULT

83       Nothing.
84

PROTOCOLS

86       Used for all protocols with headers or meta-data  concept:  HTTP,  FTP,
87       POP3, IMAP, SMTP and more.
88

EXAMPLE

90       static size_t header_callback(char *buffer, size_t size,
91                                     size_t nitems, void *userdata)
92       {
93         /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
94         /* 'userdata' is set with CURLOPT_HEADERDATA */
95         return nitems * size;
96       }
97
98       CURL *curl = curl_easy_init();
99       if(curl) {
100         curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
101
102         curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
103
104         curl_easy_perform(curl);
105       }
106

AVAILABILITY

108       Always
109

RETURN VALUE

111       Returns CURLE_OK
112

SEE ALSO

114       curl_easy_header(3), CURLOPT_HEADERDATA(3), CURLOPT_WRITEFUNCTION(3),
115
116
117
118ibcurl 8.2.1                    April 26, 2023       CURLOPT_HEADERFUNCTION(3)
Impressum