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)

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)

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: Sun, 25 Sep 2022 19:23:12 GMT
#> access-control-allow-origin: *
#> etag: W/"6330aaa0-4c8b"
#> expires: Sun, 25 Sep 2022 19:35:09 GMT
#> cache-control: max-age=600
#> content-encoding: gzip
#> x-proxy-cache: MISS
#> x-github-request-id: 1AFC:5B0B:16D41A2:208470C:6330AB15
#> accept-ranges: bytes
#> date: Sun, 25 Sep 2022 19:25:25 GMT
#> via: 1.1 varnish
#> age: 16
#> x-served-by: cache-iad-kiad7000046-IAD
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1664133926.991430,VS0,VE1
#> vary: Accept-Encoding
#> x-fastly-request-id: 1e3060c71fbf497cae6290b7b03cec34f3f84d8d
#> content-length: 5254
resp %>% resp_headers("x-")
#> <httr2_headers>
#> x-proxy-cache: MISS
#> x-github-request-id: 1AFC:5B0B:16D41A2:208470C:6330AB15
#> x-served-by: cache-iad-kiad7000046-IAD
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1664133926.991430,VS0,VE1
#> x-fastly-request-id: 1e3060c71fbf497cae6290b7b03cec34f3f84d8d

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