Parse response header data as returned by curl_fetch, either as a set of strings or into a named list.
Details
The parse_headers_list function parses the headers into a normalized (lowercase field names, trimmed whitespace) named list.
If a request has followed redirects, the data can contain multiple sets of headers. When multiple = TRUE, the function returns a list with the response headers for each request. By default it only returns the headers of the final request.
Examples
req <- curl_fetch_memory("https://hb.cran.dev/redirect/3")
parse_headers(req$headers)
#> [1] "HTTP/2 200 "
#> [2] "date: Fri, 30 Oct 2026 00:24:48 GMT"
#> [3] "content-type: application/json"
#> [4] "server: cloudflare"
#> [5] "access-control-allow-origin: *"
#> [6] "access-control-allow-credentials: true"
#> [7] "x-powered-by: Flask"
#> [8] "x-processed-time: 0"
#> [9] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"
#> [10] "cf-cache-status: DYNAMIC"
#> [11] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=8zrjfx1ULAKdh8HNH6PnEds6572X1tUFYq1Zz14tB1mk3dAq1RHiDttNbYCmSlJAg9Qo%2FLqp2pZNOS8FWrPA%2Fg0VZFam9AGq7%2B0IrvNzf42WJ6Z9%2FusF69an1rmYwQ%3D%3D\"}]}"
#> [12] "content-encoding: zstd"
#> [13] "cf-ray: a5264776cae70cf6-IAD"
#> [14] "alt-svc: h3=\":443\"; ma=86400"
parse_headers(req$headers, multiple = TRUE)
#> [[1]]
#> [1] "HTTP/2 302 "
#> [2] "date: Fri, 30 Oct 2026 00:24:48 GMT"
#> [3] "content-type: text/html; charset=utf-8"
#> [4] "server: cloudflare"
#> [5] "location: /relative-redirect/2"
#> [6] "access-control-allow-origin: *"
#> [7] "access-control-allow-credentials: true"
#> [8] "x-powered-by: Flask"
#> [9] "x-processed-time: 0"
#> [10] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"
#> [11] "cf-cache-status: DYNAMIC"
#> [12] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=Frj8TjhaHR6Io%2B4VV0lTi4UOADOYHgcfRsDYrYNSr6krDICL95Hx6GUxxdUPhE0WiHUyrPuUT81%2FjRlZW91Q5JWt3eR6k4EINxg%2FSOww6CkDOx7qqX0i5mwr732T6w%3D%3D\"}]}"
#> [13] "cf-ray: a526477618220cf6-IAD"
#> [14] "alt-svc: h3=\":443\"; ma=86400"
#>
#> [[2]]
#> [1] "HTTP/2 302 "
#> [2] "date: Fri, 30 Oct 2026 00:24:48 GMT"
#> [3] "content-type: text/html; charset=utf-8"
#> [4] "server: cloudflare"
#> [5] "location: /relative-redirect/1"
#> [6] "access-control-allow-origin: *"
#> [7] "access-control-allow-credentials: true"
#> [8] "x-powered-by: Flask"
#> [9] "x-processed-time: 0"
#> [10] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"
#> [11] "cf-cache-status: DYNAMIC"
#> [12] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=y30TmB9%2BWjhasOBqL%2BqPmFtTB9aQnqAZoNNVZltF16QiiTAqixrSX1Vba9i8CaYgnepeQNdA90EMYont4ZtmueL2i7O36NZrhJYczbWraEZeUARpSgngO5yHTsJtKQ%3D%3D\"}]}"
#> [13] "cf-ray: a526477659330cf6-IAD"
#> [14] "alt-svc: h3=\":443\"; ma=86400"
#>
#> [[3]]
#> [1] "HTTP/2 302 "
#> [2] "date: Fri, 30 Oct 2026 00:24:48 GMT"
#> [3] "content-type: text/html; charset=utf-8"
#> [4] "server: cloudflare"
#> [5] "location: /get"
#> [6] "access-control-allow-origin: *"
#> [7] "access-control-allow-credentials: true"
#> [8] "x-powered-by: Flask"
#> [9] "x-processed-time: 0"
#> [10] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"
#> [11] "cf-cache-status: DYNAMIC"
#> [12] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=bcY8CkgKCVIIjfgzL%2BEslZD51fupAngKNqsiIPKwl85w9o1uodulyI0MLdvHjUQxEQZbFZUCBibA0P7taS4w%2F6eXMiCMbxV10YM7RqxH8gkgzH5D9okzcAmHenNT6Q%3D%3D\"}]}"
#> [13] "cf-ray: a526477689ed0cf6-IAD"
#> [14] "alt-svc: h3=\":443\"; ma=86400"
#>
#> [[4]]
#> [1] "HTTP/2 200 "
#> [2] "date: Fri, 30 Oct 2026 00:24:48 GMT"
#> [3] "content-type: application/json"
#> [4] "server: cloudflare"
#> [5] "access-control-allow-origin: *"
#> [6] "access-control-allow-credentials: true"
#> [7] "x-powered-by: Flask"
#> [8] "x-processed-time: 0"
#> [9] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"
#> [10] "cf-cache-status: DYNAMIC"
#> [11] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=8zrjfx1ULAKdh8HNH6PnEds6572X1tUFYq1Zz14tB1mk3dAq1RHiDttNbYCmSlJAg9Qo%2FLqp2pZNOS8FWrPA%2Fg0VZFam9AGq7%2B0IrvNzf42WJ6Z9%2FusF69an1rmYwQ%3D%3D\"}]}"
#> [12] "content-encoding: zstd"
#> [13] "cf-ray: a5264776cae70cf6-IAD"
#> [14] "alt-svc: h3=\":443\"; ma=86400"
#>
# Parse into named list
parse_headers_list(req$headers)
#> $date
#> [1] "Fri, 30 Oct 2026 00:24:48 GMT"
#>
#> $`content-type`
#> [1] "application/json"
#>
#> $server
#> [1] "cloudflare"
#>
#> $`access-control-allow-origin`
#> [1] "*"
#>
#> $`access-control-allow-credentials`
#> [1] "true"
#>
#> $`x-powered-by`
#> [1] "Flask"
#>
#> $`x-processed-time`
#> [1] "0"
#>
#> $nel
#> [1] "{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"
#>
#> $`cf-cache-status`
#> [1] "DYNAMIC"
#>
#> $`report-to`
#> [1] "{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=8zrjfx1ULAKdh8HNH6PnEds6572X1tUFYq1Zz14tB1mk3dAq1RHiDttNbYCmSlJAg9Qo%2FLqp2pZNOS8FWrPA%2Fg0VZFam9AGq7%2B0IrvNzf42WJ6Z9%2FusF69an1rmYwQ%3D%3D\"}]}"
#>
#> $`content-encoding`
#> [1] "zstd"
#>
#> $`cf-ray`
#> [1] "a5264776cae70cf6-IAD"
#>
#> $`alt-svc`
#> [1] "h3=\":443\"; ma=86400"
#>