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. The size of the data pointed to by buffer is size
27 multiplied with nmemb. Do not assume that the header line is zero ter‐
28 minated!
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
37 return 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.
41
42 If this option is not set, or if it is set to NULL, but CURLOPT_HEADER‐
43 DATA(3) is set to anything but NULL, the function used to accept
44 response data will be used instead. That is, it will be the function
45 specified with CURLOPT_WRITEFUNCTION(3), or if it is not specified or
46 NULL - the default, stream-writing function.
47
48 It's important to note that the callback will be invoked for the head‐
49 ers of all responses received after initiating a request and not just
50 the final response. This includes all responses which occur during
51 authentication negotiation. If you need to operate on only the headers
52 from the final response, you will need to collect headers in the call‐
53 back yourself and use HTTP status lines, for example, to delimit
54 response boundaries.
55
56 When a server sends a chunked encoded transfer, it may contain a
57 trailer. That trailer is identical to an HTTP header and if such a
58 trailer is received it is passed to the application using this callback
59 as well. There are several ways to detect it being a trailer and not an
60 ordinary header: 1) it comes after the response-body. 2) it comes after
61 the final header line (CR LF) 3) a Trailer: header among the regular
62 response-headers mention what header(s) to expect in the trailer.
63
64 For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function will
65 get called with the server responses to the commands that libcurl
66 sends.
67
69 libcurl does not unfold HTTP "folded headers" (deprecated since RFC
70 7230). A folded header is a header that continues on a subsequent line
71 and starts with a whitespace. Such folds will be passed to the header
72 callback as a separate one, although strictly it is just a continuation
73 of the previous line.
74
76 Nothing.
77
79 Used for all protocols with headers or meta-data concept: HTTP, FTP,
80 POP3, IMAP, SMTP and more.
81
83 static size_t header_callback(char *buffer, size_t size,
84 size_t nitems, void *userdata)
85 {
86 /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
87 /* 'userdata' is set with CURLOPT_HEADERDATA */
88 return nitems * size;
89 }
90
91 CURL *curl = curl_easy_init();
92 if(curl) {
93 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
94
95 curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
96
97 curl_easy_perform(curl);
98 }
99
101 Always
102
104 Returns CURLE_OK
105
107 CURLOPT_HEADERDATA(3), CURLOPT_WRITEFUNCTION(3),
108
109
110
111libcurl 7.61.1 August 11, 2018 CURLOPT_HEADERFUNCTION(3)