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 andNULL
otherwise.resp_header_exists()
returnsTRUE
orFALSE
.
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, 16 Jun 2025 19:35:32 GMT
#> access-control-allow-origin: *
#> etag: W/"68507204-4b42"
#> expires: Mon, 16 Jun 2025 19:53:53 GMT
#> cache-control: max-age=600
#> content-encoding: gzip
#> x-proxy-cache: MISS
#> x-github-request-id: 8CDE:160F09:28E1F5:2A5894:685073F9
#> accept-ranges: bytes
#> date: Mon, 16 Jun 2025 19:44:10 GMT
#> via: 1.1 varnish
#> age: 18
#> x-served-by: cache-sjc1000102-SJC
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1750103051.804672,VS0,VE1
#> vary: Accept-Encoding
#> x-fastly-request-id: ef395e33149830cc2e33e336a02552886f43b34c
#> content-length: 4746
resp |> resp_headers("x-")
#> <httr2_headers>
#> x-proxy-cache: MISS
#> x-github-request-id: 8CDE:160F09:28E1F5:2A5894:685073F9
#> x-served-by: cache-sjc1000102-SJC
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1750103051.804672,VS0,VE1
#> x-fastly-request-id: ef395e33149830cc2e33e336a02552886f43b34c
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