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

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: Tue, 08 Jul 2025 20:26:04 GMT"                                                                                                                                                                                                       
#>  [3] "content-type: application/json"                                                                                                                                                                                                            
#>  [4] "server: cloudflare"                                                                                                                                                                                                                        
#>  [5] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"                                                                                                                                                               
#>  [6] "access-control-allow-origin: *"                                                                                                                                                                                                            
#>  [7] "access-control-allow-credentials: true"                                                                                                                                                                                                    
#>  [8] "x-powered-by: Flask"                                                                                                                                                                                                                       
#>  [9] "x-processed-time: 0"                                                                                                                                                                                                                       
#> [10] "cf-cache-status: DYNAMIC"                                                                                                                                                                                                                  
#> [11] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=md7gEGOiwJsWC8XZGxo7J%2FcsJyXclIXBeGOmv1zs%2FmkuYcy6mN2jg5fBtYQEDGO%2F4n0bLiz1zxEwK8FhYNocHbp323uwDHwMsg%3D%3D\"}]}"
#> [12] "content-encoding: zstd"                                                                                                                                                                                                                    
#> [13] "cf-ray: 95c25083882d7118-YYZ"                                                                                                                                                                                                              
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                            
parse_headers(req$headers, multiple = TRUE)
#> [[1]]
#>  [1] "HTTP/2 302 "                                                                                                                                                                                                                                     
#>  [2] "date: Tue, 08 Jul 2025 20:26:04 GMT"                                                                                                                                                                                                             
#>  [3] "content-type: text/html; charset=utf-8"                                                                                                                                                                                                          
#>  [4] "server: cloudflare"                                                                                                                                                                                                                              
#>  [5] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"                                                                                                                                                                     
#>  [6] "location: /relative-redirect/2"                                                                                                                                                                                                                  
#>  [7] "access-control-allow-origin: *"                                                                                                                                                                                                                  
#>  [8] "access-control-allow-credentials: true"                                                                                                                                                                                                          
#>  [9] "x-powered-by: Flask"                                                                                                                                                                                                                             
#> [10] "x-processed-time: 0"                                                                                                                                                                                                                             
#> [11] "cf-cache-status: DYNAMIC"                                                                                                                                                                                                                        
#> [12] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=Cwe0NioDLdSTV1vHsDtwAmPkMJe%2F%2B37TY7%2BLtHrsf%2BiZMeeboiwiAPO2rua%2Bl80GowtlxtMNKkFG%2F8PNGV7cG0PPqxxtm0eemw%3D%3D\"}]}"
#> [13] "cf-ray: 95c25082bf7c7118-YYZ"                                                                                                                                                                                                                    
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                                  
#> 
#> [[2]]
#>  [1] "HTTP/2 302 "                                                                                                                                                                                                                         
#>  [2] "date: Tue, 08 Jul 2025 20:26:04 GMT"                                                                                                                                                                                                 
#>  [3] "content-type: text/html; charset=utf-8"                                                                                                                                                                                              
#>  [4] "server: cloudflare"                                                                                                                                                                                                                  
#>  [5] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"                                                                                                                                                         
#>  [6] "location: /relative-redirect/1"                                                                                                                                                                                                      
#>  [7] "access-control-allow-origin: *"                                                                                                                                                                                                      
#>  [8] "access-control-allow-credentials: true"                                                                                                                                                                                              
#>  [9] "x-powered-by: Flask"                                                                                                                                                                                                                 
#> [10] "x-processed-time: 0"                                                                                                                                                                                                                 
#> [11] "cf-cache-status: DYNAMIC"                                                                                                                                                                                                            
#> [12] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=Wg7SC2vp5abJQezi2AoA3BamNFrehZwaHKFTdQrz4fcthPhgNvYVqnb3ZsiCpHAq4xkQUC5EfvsorXRbg2ll76TN0AvFD0lBkA%3D%3D\"}]}"
#> [13] "cf-ray: 95c250830fbf7118-YYZ"                                                                                                                                                                                                        
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                      
#> 
#> [[3]]
#>  [1] "HTTP/2 302 "                                                                                                                                                                                                                               
#>  [2] "date: Tue, 08 Jul 2025 20:26:04 GMT"                                                                                                                                                                                                       
#>  [3] "content-type: text/html; charset=utf-8"                                                                                                                                                                                                    
#>  [4] "server: cloudflare"                                                                                                                                                                                                                        
#>  [5] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"                                                                                                                                                               
#>  [6] "location: /get"                                                                                                                                                                                                                            
#>  [7] "access-control-allow-origin: *"                                                                                                                                                                                                            
#>  [8] "access-control-allow-credentials: true"                                                                                                                                                                                                    
#>  [9] "x-powered-by: Flask"                                                                                                                                                                                                                       
#> [10] "x-processed-time: 0"                                                                                                                                                                                                                       
#> [11] "cf-cache-status: DYNAMIC"                                                                                                                                                                                                                  
#> [12] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=jg9P2JCJ%2FxZ87OvrzKLDhPvHA4%2BeMg5UEtPoTH1MKK96lytgH61%2B3zUjTdh70yHzt0jcTXPYKNh1wETYZZb7yQCF73PYdGx2hg%3D%3D\"}]}"
#> [13] "cf-ray: 95c250834ff97118-YYZ"                                                                                                                                                                                                              
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                            
#> 
#> [[4]]
#>  [1] "HTTP/2 200 "                                                                                                                                                                                                                               
#>  [2] "date: Tue, 08 Jul 2025 20:26:04 GMT"                                                                                                                                                                                                       
#>  [3] "content-type: application/json"                                                                                                                                                                                                            
#>  [4] "server: cloudflare"                                                                                                                                                                                                                        
#>  [5] "nel: {\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"                                                                                                                                                               
#>  [6] "access-control-allow-origin: *"                                                                                                                                                                                                            
#>  [7] "access-control-allow-credentials: true"                                                                                                                                                                                                    
#>  [8] "x-powered-by: Flask"                                                                                                                                                                                                                       
#>  [9] "x-processed-time: 0"                                                                                                                                                                                                                       
#> [10] "cf-cache-status: DYNAMIC"                                                                                                                                                                                                                  
#> [11] "report-to: {\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=md7gEGOiwJsWC8XZGxo7J%2FcsJyXclIXBeGOmv1zs%2FmkuYcy6mN2jg5fBtYQEDGO%2F4n0bLiz1zxEwK8FhYNocHbp323uwDHwMsg%3D%3D\"}]}"
#> [12] "content-encoding: zstd"                                                                                                                                                                                                                    
#> [13] "cf-ray: 95c25083882d7118-YYZ"                                                                                                                                                                                                              
#> [14] "alt-svc: h3=\":443\"; ma=86400"                                                                                                                                                                                                            
#> 

# Parse into named list
parse_headers_list(req$headers)
#> $date
#> [1] "Tue, 08 Jul 2025 20:26:04 GMT"
#> 
#> $`content-type`
#> [1] "application/json"
#> 
#> $server
#> [1] "cloudflare"
#> 
#> $nel
#> [1] "{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}"
#> 
#> $`access-control-allow-origin`
#> [1] "*"
#> 
#> $`access-control-allow-credentials`
#> [1] "true"
#> 
#> $`x-powered-by`
#> [1] "Flask"
#> 
#> $`x-processed-time`
#> [1] "0"
#> 
#> $`cf-cache-status`
#> [1] "DYNAMIC"
#> 
#> $`report-to`
#> [1] "{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=md7gEGOiwJsWC8XZGxo7J%2FcsJyXclIXBeGOmv1zs%2FmkuYcy6mN2jg5fBtYQEDGO%2F4n0bLiz1zxEwK8FhYNocHbp323uwDHwMsg%3D%3D\"}]}"
#> 
#> $`content-encoding`
#> [1] "zstd"
#> 
#> $`cf-ray`
#> [1] "95c25083882d7118-YYZ"
#> 
#> $`alt-svc`
#> [1] "h3=\":443\"; ma=86400"
#>