1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: CURLOPT_TRAILERFUNCTION
5Section: 3
6Source: libcurl
7See-also:
8  - CURLOPT_TRAILERDATA (3)
9  - CURLOPT_WRITEFUNCTION (3)
10Protocol:
11  - HTTP
12---
13
14# NAME
15
16CURLOPT_TRAILERFUNCTION - callback for sending trailing headers
17
18# SYNOPSIS
19
20~~~c
21#include <curl.h>
22
23int curl_trailer_callback(struct curl_slist ** list, void *userdata);
24
25CURLcode curl_easy_setopt(CURL *handle, CURLOPT_TRAILERFUNCTION,
26                          curl_trailer_callback *func);
27~~~
28
29# DESCRIPTION
30
31Pass a pointer to a callback function.
32
33This callback function is called once right before sending the final CR LF in
34an HTTP chunked transfer to fill a list of trailing headers to be sent before
35finishing the HTTP transfer.
36
37You can set the userdata argument with the CURLOPT_TRAILERDATA(3)
38option.
39
40The trailing headers included in the linked list must not be CRLF-terminated,
41because libcurl adds the appropriate line termination characters after each
42header item.
43
44If you use curl_slist_append(3) to add trailing headers to the *curl_slist*
45then libcurl duplicates the strings, and frees the *curl_slist* once the
46trailers have been sent.
47
48If one of the trailing header fields is not formatted correctly it is ignored
49and an info message is emitted.
50
51The return value can either be **CURL_TRAILERFUNC_OK** or
52**CURL_TRAILERFUNC_ABORT** which would respectively instruct libcurl to
53either continue with sending the trailers or to abort the request.
54
55If you set this option to NULL, then the transfer proceeds as usual
56without any interruptions.
57
58# DEFAULT
59
60NULL
61
62# EXAMPLE
63~~~c
64static int trailer_cb(struct curl_slist **tr, void *data)
65{
66  /* libcurl frees the list */
67  *tr = curl_slist_append(*tr, "My-super-awesome-trailer: trailer-stuff");
68  return CURL_TRAILERFUNC_OK;
69}
70
71int main(void)
72{
73  CURL *curl = curl_easy_init();
74  if(curl) {
75    CURLcode res;
76
77    /* Set the URL of the request */
78    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
79    /* Now set it as a put */
80    curl_easy_setopt(curl, CURLOPT_PUT, 1L);
81
82    /* Assuming we have a function that returns the data to be pushed
83       Let that function be read_cb */
84    curl_easy_setopt(curl, CURLOPT_READFUNCTION, trailer_cb);
85
86    struct curl_slist *headers = NULL;
87    headers = curl_slist_append(headers, "Trailer: My-super-awesome-trailer");
88    res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
89
90    /* Set the trailers filling callback */
91    curl_easy_setopt(curl, CURLOPT_TRAILERFUNCTION, trailer_cb);
92
93    /* Perform the transfer */
94    res = curl_easy_perform(curl);
95
96    curl_easy_cleanup(curl);
97
98    curl_slist_free_all(headers);
99  }
100}
101~~~
102# AVAILABILITY
103
104This option was added in curl 7.64.0 and is present if HTTP support is enabled.
105
106# RETURN VALUE
107
108Returns CURLE_OK.
109