Skip to contents

Parse response header data as returned by curl_fetch, either as a set of strings or into a named list.

Usage

parse_headers(txt, multiple = FALSE)

parse_headers_list(txt)

Arguments

txt

raw or character vector with the header data

multiple

parse multiple sets of headers separated by a blank line. See details.

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, 28 Aug 2026 21:16:10 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=JDUZ8bER0Bg%2FufS0II50YMpkdMkXgrKRg1cAap1fxpL9GcI%2Bi7ACA9H0IEYIxdmDU%2By5FVSIH7%2Faz8aEYex1Y9WFoYuoXDicywnc%2FA%2B19HRBUE0dffHGKYOXucwB5w%3D%3D\"}]}"
#> [12] "content-encoding: zstd"                                                                                                                                                                                                                                                      
#> [13] "cf-ray: a32655e3fb1b1321-SJC"                                                                                                                                                                                                                                                
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                                                              
parse_headers(req$headers, multiple = TRUE)
#> [[1]]
#>  [1] "HTTP/2 302 "                                                                                                                                                                                                                                                                 
#>  [2] "date: Fri, 28 Aug 2026 21:16:10 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=j4KONKCI%2FiNE%2FNcidWFmM%2Fq7Qt%2F5FFZ36x%2B9BIkOnBIikR7aq0wuDEwXi%2FRLJAD6wZLXZnRxUW4SWg2Aa53rBWMo9B1U4YFikSgmrR3STNI34eYt6M8IJn8iktOtJQ%3D%3D\"}]}"
#> [13] "cf-ray: a32655e26d511321-SJC"                                                                                                                                                                                                                                                
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                                                              
#> 
#> [[2]]
#>  [1] "HTTP/2 302 "                                                                                                                                                                                                                                                                 
#>  [2] "date: Fri, 28 Aug 2026 21:16:10 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=rnmX8OL2ItH60uXZ95up4VaYi%2FY96fOlyDqetFlR5NIi6EO7Te3bL3iS%2FOuX1%2B1iUkbu4xvmUZ%2B4%2Blvri4kkSY2Rv9%2FP0c19K9aBz4lLdouqFc2Fpj8TYMFpRpTn9Q%3D%3D\"}]}"
#> [13] "cf-ray: a32655e2ef2b1321-SJC"                                                                                                                                                                                                                                                
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                                                              
#> 
#> [[3]]
#>  [1] "HTTP/2 302 "                                                                                                                                                                                                                                                               
#>  [2] "date: Fri, 28 Aug 2026 21:16:10 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=nB4Pq4zz6qN%2FcXDL1Oym3JNzoD8NGyUj9HfcgdGLGcAIIOiYj%2BYCc%2B0xUFRrIe43XheCyEXa%2Bv3KUUfyWyj0LXuJKHK1Kiz7YjgerBjTgn%2BwA1ETylPnQYgWYCe7cg%3D%3D\"}]}"
#> [13] "cf-ray: a32655e379661321-SJC"                                                                                                                                                                                                                                              
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                                                            
#> 
#> [[4]]
#>  [1] "HTTP/2 200 "                                                                                                                                                                                                                                                                 
#>  [2] "date: Fri, 28 Aug 2026 21:16:10 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=JDUZ8bER0Bg%2FufS0II50YMpkdMkXgrKRg1cAap1fxpL9GcI%2Bi7ACA9H0IEYIxdmDU%2By5FVSIH7%2Faz8aEYex1Y9WFoYuoXDicywnc%2FA%2B19HRBUE0dffHGKYOXucwB5w%3D%3D\"}]}"
#> [12] "content-encoding: zstd"                                                                                                                                                                                                                                                      
#> [13] "cf-ray: a32655e3fb1b1321-SJC"                                                                                                                                                                                                                                                
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                                                              
#> 

# Parse into named list
parse_headers_list(req$headers)
#> $date
#> [1] "Fri, 28 Aug 2026 21:16:10 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=JDUZ8bER0Bg%2FufS0II50YMpkdMkXgrKRg1cAap1fxpL9GcI%2Bi7ACA9H0IEYIxdmDU%2By5FVSIH7%2Faz8aEYex1Y9WFoYuoXDicywnc%2FA%2B19HRBUE0dffHGKYOXucwB5w%3D%3D\"}]}"
#> 
#> $`content-encoding`
#> [1] "zstd"
#> 
#> $`cf-ray`
#> [1] "a32655e3fb1b1321-SJC"
#> 
#> $`alt-svc`
#> [1] "h3=\":443\"; ma=86400"
#>