Skip to content
  • resp_headers() retrieves a list of all headers.

  • resp_header() retrieves a single header.

  • resp_header_exists() checks if a header is present.

Usage

resp_headers(resp, filter = NULL)

resp_header(resp, header, default = NULL)

resp_header_exists(resp, header)

Arguments

resp

An HTTP response object, as created by req_perform().

filter

A regular expression used to filter the header names. NULL, the default, returns all headers.

header

Header name (case insensitive)

default

Default value to use if header doesn't exist.

Value

  • resp_headers() returns a list.

  • resp_header() returns a string if the header exists and NULL otherwise.

  • resp_header_exists() returns TRUE or FALSE.

Examples

resp <- request("https://httr2.r-lib.org") |> req_perform()
resp |> resp_headers()
#> <httr2_headers>
#> server: GitHub.com
#> content-type: text/html; charset=utf-8
#> last-modified: Mon, 15 Jul 2024 20:51:25 GMT
#> access-control-allow-origin: *
#> etag: W/"66958bcd-4e7b"
#> expires: Tue, 16 Jul 2024 12:34:23 GMT
#> cache-control: max-age=600
#> content-encoding: gzip
#> x-hosts-log-append: pages_hosts_ips:{ [1] = 10.0.18.190,[2] = 10.0.3.132,[3] = 10.0.34.102,}
#> x-proxy-cache: MISS
#> x-github-request-id: D827:1CCA5F:2F5F882:382E8C8:66966674
#> accept-ranges: bytes
#> date: Tue, 16 Jul 2024 12:24:42 GMT
#> via: 1.1 varnish
#> age: 19
#> x-served-by: cache-chi-kigq8000047-CHI
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1721132682.270281,VS0,VE0
#> vary: Accept-Encoding
#> x-fastly-request-id: 37e07098626ffc60a3c55241e395dba29da5293b
#> content-length: 5418
resp |> resp_headers("x-")
#> <httr2_headers>
#> x-hosts-log-append: pages_hosts_ips:{ [1] = 10.0.18.190,[2] = 10.0.3.132,[3] = 10.0.34.102,}
#> x-proxy-cache: MISS
#> x-github-request-id: D827:1CCA5F:2F5F882:382E8C8:66966674
#> x-served-by: cache-chi-kigq8000047-CHI
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1721132682.270281,VS0,VE0
#> x-fastly-request-id: 37e07098626ffc60a3c55241e395dba29da5293b

resp |> resp_header_exists("server")
#> [1] TRUE
resp |> resp_header("server")
#> [1] "GitHub.com"
# Headers are case insensitive
resp |> resp_header("SERVER")
#> [1] "GitHub.com"

# Returns NULL if header doesn't exist
resp |> resp_header("this-header-doesnt-exist")
#> NULL