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

A httr2 response object, 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: Tue, 21 Jan 2025 18:09:41 GMT
#> access-control-allow-origin: *
#> etag: W/"678fe2e5-4983"
#> expires: Wed, 22 Jan 2025 16:21:49 GMT
#> cache-control: max-age=600
#> content-encoding: gzip
#> x-proxy-cache: MISS
#> x-github-request-id: 500D:3D2BDE:10B3E7:113A45:679118C1
#> accept-ranges: bytes
#> date: Wed, 22 Jan 2025 16:12:09 GMT
#> via: 1.1 varnish
#> age: 20
#> x-served-by: cache-pao-kpao1770061-PAO
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1737562329.364980,VS0,VE1
#> vary: Accept-Encoding
#> x-fastly-request-id: 814f329dd9dc1f75fe1d4dd77ee3cabef0ef6b60
#> content-length: 4638
resp |> resp_headers("x-")
#> <httr2_headers>
#> x-proxy-cache: MISS
#> x-github-request-id: 500D:3D2BDE:10B3E7:113A45:679118C1
#> x-served-by: cache-pao-kpao1770061-PAO
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1737562329.364980,VS0,VE1
#> x-fastly-request-id: 814f329dd9dc1f75fe1d4dd77ee3cabef0ef6b60

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