xref: /curl/docs/libcurl/curl_slist_append.md (revision 5a488251)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_slist_append
5Section: 3
6Source: libcurl
7See-also:
8  - curl_slist_free_all (3)
9Protocol:
10  - All
11Added-in: 7.1
12---
13
14# NAME
15
16curl_slist_append - add a string to an slist
17
18# SYNOPSIS
19
20~~~c
21#include <curl/curl.h>
22
23struct curl_slist *curl_slist_append(struct curl_slist *list,
24                                     const char *string);
25~~~
26
27# DESCRIPTION
28
29curl_slist_append(3) appends a string to a linked list of strings. The
30existing **list** should be passed as the first argument and the new list is
31returned from this function. Pass in NULL in the **list** argument to create
32a new list. The specified **string** has been appended when this function
33returns. curl_slist_append(3) copies the string.
34
35The list should be freed again (after usage) with
36curl_slist_free_all(3).
37
38# %PROTOCOLS%
39
40# EXAMPLE
41
42~~~c
43int main(void)
44{
45  CURL *handle;
46  struct curl_slist *slist = NULL;
47  struct curl_slist *temp = NULL;
48
49  slist = curl_slist_append(slist, "pragma:");
50
51  if(!slist)
52    return -1;
53
54  temp = curl_slist_append(slist, "Accept:");
55
56  if(!temp) {
57    curl_slist_free_all(slist);
58    return -1;
59  }
60
61  slist = temp;
62
63  curl_easy_setopt(handle, CURLOPT_HTTPHEADER, slist);
64
65  curl_easy_perform(handle);
66
67  curl_slist_free_all(slist); /* free the list again */
68}
69~~~
70
71# %AVAILABILITY%
72
73# RETURN VALUE
74
75A null pointer is returned if anything went wrong, otherwise the new list
76pointer is returned. To avoid overwriting an existing non-empty list on
77failure, the new list should be returned to a temporary variable which can
78be tested for NULL before updating the original list pointer.
79