• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1---
2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_easy_nextheader
5Section: 3
6Source: libcurl
7See-also:
8  - curl_easy_header (3)
9  - curl_easy_perform (3)
10---
11
12# NAME
13
14curl_easy_nextheader - get the next HTTP header
15
16# SYNOPSIS
17
18~~~c
19#include <curl/curl.h>
20
21struct curl_header *curl_easy_nextheader(CURL *easy,
22                                         unsigned int origin,
23                                         int request,
24                                         struct curl_header *prev);
25~~~
26
27# DESCRIPTION
28
29This function lets an application iterate over all previously received HTTP
30headers.
31
32The *origin* argument is for specifying which headers to receive, as a single
33HTTP transfer might provide headers from several different places and they may
34then have different importance to the user and headers using the same name
35might be used. The *origin* is a bitmask for what header sources you want. See
36the curl_easy_header(3) man page for the origin descriptions.
37
38The *request* argument tells libcurl from which request you want headers
39from. A single transfer might consist of a series of HTTP requests and this
40argument lets you specify which particular individual request you want the
41headers from. 0 being the first request and then the number increases for
42further redirects or when multi-state authentication is used. Passing in -1 is
43a shortcut to "the last" request in the series, independently of the actual
44amount of requests used.
45
46It is suggested that you pass in the same **origin** and **request** when
47iterating over a range of headers as changing the value mid-loop might give
48you unexpected results.
49
50If *prev* is NULL, this function returns a pointer to the first header stored
51within the given scope (origin + request).
52
53If *prev* is a pointer to a previously returned header struct,
54curl_easy_nextheader(3) returns a pointer the next header stored within the
55given scope. This way, an application can iterate over all available headers.
56
57The memory for the struct this points to, is owned and managed by libcurl and
58is associated with the easy handle. Applications must copy the data if they
59want it to survive subsequent API calls or the life-time of the easy handle.
60
61# EXAMPLE
62
63~~~c
64int main(void)
65{
66  struct curl_header *prev = NULL;
67  struct curl_header *h;
68
69  CURL *curl = curl_easy_init();
70  if(curl) {
71    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
72    curl_easy_perform(curl);
73
74    /* extract the normal headers from the first request */
75    while((h = curl_easy_nextheader(curl, CURLH_HEADER, 0, prev))) {
76      printf("%s: %s\n", h->name, h->value);
77      prev = h;
78    }
79
80    /* extract the normal headers + 1xx + trailers from the last request */
81    unsigned int origin = CURLH_HEADER| CURLH_1XX | CURLH_TRAILER;
82    while((h = curl_easy_nextheader(curl, origin, -1, prev))) {
83      printf("%s: %s\n", h->name, h->value);
84      prev = h;
85    }
86  }
87}
88~~~
89
90# AVAILABILITY
91
92Added in 7.83.0. Officially supported since 7.84.0.
93
94# RETURN VALUE
95
96This function returns the next header, or NULL when there are no more
97(matching) headers or an error occurred.
98
99If this function returns NULL when *prev* was set to NULL, then there are no
100headers available within the scope to return.
101