1CURLOPT_HEADERFUNCTION(3) curl_easy_setopt options CURLOPT_HEADERFUNCTION(3)
2
3
4
6 CURLOPT_HEADERFUNCTION - callback that receives header data
7
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
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 very easy using this. buffer points to the delivered data, and the size
27 of that data is nitems; size is always 1. Do not assume that the header
28 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 This callback function must return the number of bytes actually taken
34 care of. If that amount differs from the amount passed in to your
35 function, it'll signal an error to the library. This will cause the
36 transfer to get aborted and the libcurl function in progress will re‐
37 turn CURLE_WRITE_ERROR.
38
39 A complete HTTP header that is passed to this function can be up to
40 CURL_MAX_HTTP_HEADER (100K) bytes and includes the final line termina‐
41 tor.
42
43 If this option is not set, or if it is set to NULL, but CURLOPT_HEADER‐
44 DATA(3) is set to anything but NULL, the function used to accept re‐
45 sponse data will be used instead. That is, it will be the function
46 specified with CURLOPT_WRITEFUNCTION(3), or if it is not specified or
47 NULL - the default, stream-writing function.
48
49 It's important to note that the callback will be invoked for the head‐
50 ers of all responses received after initiating a request and not just
51 the final response. This includes all responses which occur during au‐
52 thentication negotiation. If you need to operate on only the headers
53 from the final response, you will need to collect headers in the call‐
54 back yourself and use HTTP status lines, for example, to delimit re‐
55 sponse boundaries.
56
57 For an HTTP transfer, the status line and the blank line preceding the
58 response body are both included as headers and passed to this function.
59
60 When a server sends a chunked encoded transfer, it may contain a
61 trailer. That trailer is identical to an HTTP header and if such a
62 trailer is received it is passed to the application using this callback
63 as well. There are several ways to detect it being a trailer and not an
64 ordinary header: 1) it comes after the response-body. 2) it comes after
65 the final header line (CR LF) 3) a Trailer: header among the regular
66 response-headers mention what header(s) to expect in the trailer.
67
68 For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function will
69 get called with the server responses to the commands that libcurl
70 sends.
71
73 libcurl does not unfold HTTP "folded headers" (deprecated since RFC
74 7230). A folded header is a header that continues on a subsequent line
75 and starts with a whitespace. Such folds will be passed to the header
76 callback as a separate one, although strictly it is just a continuation
77 of the previous line.
78
80 Nothing.
81
83 Used for all protocols with headers or meta-data concept: HTTP, FTP,
84 POP3, IMAP, SMTP and more.
85
87 static size_t header_callback(char *buffer, size_t size,
88 size_t nitems, void *userdata)
89 {
90 /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
91 /* 'userdata' is set with CURLOPT_HEADERDATA */
92 return nitems * size;
93 }
94
95 CURL *curl = curl_easy_init();
96 if(curl) {
97 curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
98
99 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
100
101 curl_easy_perform(curl);
102 }
103
105 Always
106
108 Returns CURLE_OK
109
111 CURLOPT_HEADERDATA(3), CURLOPT_WRITEFUNCTION(3),
112
113
114
115libcurl 7.76.1 November 04, 2020 CURLOPT_HEADERFUNCTION(3)