1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_HEADERFUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_HEADERDATA (3)
9  - CURLOPT_WRITEFUNCTION (3)
10  - curl_easy_header (3)
11Protocol:
12  - HTTP
13  - FTP
14  - POP3
15  - IMAP
16  - SMTP
17Added-in: 7.7.2
18---
19
20# NAME
21
22CURLOPT_HEADERFUNCTION - callback that receives header data
23
24# SYNOPSIS
25
26~~~c
27#include <curl/curl.h>
28
29size_t header_callback(char *buffer,
30                       size_t size,
31                       size_t nitems,
32                       void *userdata);
33
34CURLcode curl_easy_setopt(CURL *handle, CURLOPT_HEADERFUNCTION,
35                          header_callback);
36~~~
37
38# DESCRIPTION
39
40Pass a pointer to your callback function, which should match the prototype
41shown above.
42
43This callback function gets invoked by libcurl as soon as it has received
44header data. The header callback is called once for each header and only
45complete header lines are passed on to the callback. Parsing headers is easy
46to do using this callback. *buffer* points to the delivered data, and the size
47of that data is *nitems*; *size* is always 1. The provided header line is not
48null-terminated. Do not modify the passed in buffer.
49
50The pointer named *userdata* is the one you set with the CURLOPT_HEADERDATA(3)
51option.
52
53Your callback should return the number of bytes actually taken care of. If
54that amount differs from the amount passed to your callback function, it
55signals an error condition to the library. This causes the transfer to get
56aborted and the libcurl function used returns *CURLE_WRITE_ERROR*.
57
58You can also abort the transfer by returning CURL_WRITEFUNC_ERROR. (7.87.0)
59
60A complete HTTP header that is passed to this function can be up to
61*CURL_MAX_HTTP_HEADER* (100K) bytes and includes the final line terminator.
62
63If this option is not set, or if it is set to NULL, but
64CURLOPT_HEADERDATA(3) is set to anything but NULL, the function used to
65accept response data is used instead. That is the function specified with
66CURLOPT_WRITEFUNCTION(3), or if it is not specified or NULL - the
67default, stream-writing function.
68
69It is important to note that the callback is invoked for the headers of all
70responses received after initiating a request and not just the final
71response. This includes all responses which occur during authentication
72negotiation. If you need to operate on only the headers from the final
73response, you need to collect headers in the callback yourself and use HTTP
74status lines, for example, to delimit response boundaries.
75
76For an HTTP transfer, the status line and the blank line preceding the response
77body are both included as headers and passed to this function.
78
79When a server sends a chunked encoded transfer, it may contain a trailer. That
80trailer is identical to an HTTP header and if such a trailer is received it is
81passed to the application using this callback as well. There are several ways
82to detect it being a trailer and not an ordinary header: 1) it comes after the
83response-body. 2) it comes after the final header line (CR LF) 3) a Trailer:
84header among the regular response-headers mention what header(s) to expect in
85the trailer.
86
87For non-HTTP protocols like FTP, POP3, IMAP and SMTP this function gets called
88with the server responses to the commands that libcurl sends.
89
90A more convenient way to get HTTP headers might be to use
91curl_easy_header(3).
92
93# LIMITATIONS
94
95libcurl does not unfold HTTP "folded headers" (deprecated since RFC 7230). A
96folded header is a header that continues on a subsequent line and starts with
97a whitespace. Such folds are passed to the header callback as separate ones,
98although strictly they are just continuations of the previous lines.
99
100# DEFAULT
101
102Nothing.
103
104# %PROTOCOLS%
105
106# EXAMPLE
107
108~~~c
109static size_t header_callback(char *buffer, size_t size,
110                              size_t nitems, void *userdata)
111{
112  /* received header is nitems * size long in 'buffer' NOT ZERO TERMINATED */
113  /* 'userdata' is set with CURLOPT_HEADERDATA */
114  return nitems * size;
115}
116
117int main(void)
118{
119  CURL *curl = curl_easy_init();
120  if(curl) {
121    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
122
123    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
124
125    curl_easy_perform(curl);
126  }
127}
128~~~
129
130# %AVAILABILITY%
131
132# RETURN VALUE
133
134Returns CURLE_OK
135