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://httpbin.org/redirect/3") parse_headers(req$headers)
#> [1] "HTTP/1.1 200 OK" #> [2] "Access-Control-Allow-Credentials: true" #> [3] "Access-Control-Allow-Origin: *" #> [4] "Content-Encoding: gzip" #> [5] "Content-Type: application/json" #> [6] "Date: Mon, 02 Dec 2019 12:01:50 GMT" #> [7] "Referrer-Policy: no-referrer-when-downgrade" #> [8] "Server: nginx" #> [9] "X-Content-Type-Options: nosniff" #> [10] "X-Frame-Options: DENY" #> [11] "X-XSS-Protection: 1; mode=block" #> [12] "Content-Length: 201" #> [13] "Connection: keep-alive"
parse_headers(req$headers, multiple = TRUE)
#> [[1]] #> [1] "HTTP/1.1 302 FOUND" #> [2] "Access-Control-Allow-Credentials: true" #> [3] "Access-Control-Allow-Origin: *" #> [4] "Content-Type: text/html; charset=utf-8" #> [5] "Date: Mon, 02 Dec 2019 12:01:50 GMT" #> [6] "Location: /relative-redirect/2" #> [7] "Referrer-Policy: no-referrer-when-downgrade" #> [8] "Server: nginx" #> [9] "X-Content-Type-Options: nosniff" #> [10] "X-Frame-Options: DENY" #> [11] "X-XSS-Protection: 1; mode=block" #> [12] "Content-Length: 247" #> [13] "Connection: keep-alive" #> #> [[2]] #> [1] "HTTP/1.1 302 FOUND" #> [2] "Access-Control-Allow-Credentials: true" #> [3] "Access-Control-Allow-Origin: *" #> [4] "Content-Type: text/html; charset=utf-8" #> [5] "Date: Mon, 02 Dec 2019 12:01:50 GMT" #> [6] "Location: /relative-redirect/1" #> [7] "Referrer-Policy: no-referrer-when-downgrade" #> [8] "Server: nginx" #> [9] "X-Content-Type-Options: nosniff" #> [10] "X-Frame-Options: DENY" #> [11] "X-XSS-Protection: 1; mode=block" #> [12] "Content-Length: 0" #> [13] "Connection: keep-alive" #> #> [[3]] #> [1] "HTTP/1.1 302 FOUND" #> [2] "Access-Control-Allow-Credentials: true" #> [3] "Access-Control-Allow-Origin: *" #> [4] "Content-Type: text/html; charset=utf-8" #> [5] "Date: Mon, 02 Dec 2019 12:01:50 GMT" #> [6] "Location: /get" #> [7] "Referrer-Policy: no-referrer-when-downgrade" #> [8] "Server: nginx" #> [9] "X-Content-Type-Options: nosniff" #> [10] "X-Frame-Options: DENY" #> [11] "X-XSS-Protection: 1; mode=block" #> [12] "Content-Length: 0" #> [13] "Connection: keep-alive" #> #> [[4]] #> [1] "HTTP/1.1 200 OK" #> [2] "Access-Control-Allow-Credentials: true" #> [3] "Access-Control-Allow-Origin: *" #> [4] "Content-Encoding: gzip" #> [5] "Content-Type: application/json" #> [6] "Date: Mon, 02 Dec 2019 12:01:50 GMT" #> [7] "Referrer-Policy: no-referrer-when-downgrade" #> [8] "Server: nginx" #> [9] "X-Content-Type-Options: nosniff" #> [10] "X-Frame-Options: DENY" #> [11] "X-XSS-Protection: 1; mode=block" #> [12] "Content-Length: 201" #> [13] "Connection: keep-alive" #>
# Parse into named list parse_headers_list(req$headers)
#> $`access-control-allow-credentials` #> [1] "true" #> #> $`access-control-allow-origin` #> [1] "*" #> #> $`content-encoding` #> [1] "gzip" #> #> $`content-type` #> [1] "application/json" #> #> $date #> [1] "Mon, 02 Dec 2019 12:01:50 GMT" #> #> $`referrer-policy` #> [1] "no-referrer-when-downgrade" #> #> $server #> [1] "nginx" #> #> $`x-content-type-options` #> [1] "nosniff" #> #> $`x-frame-options` #> [1] "DENY" #> #> $`x-xss-protection` #> [1] "1; mode=block" #> #> $`content-length` #> [1] "201" #> #> $connection #> [1] "keep-alive" #>