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