resp_headers()
retrieves a list of all headers.resp_header()
retrieves a single header.resp_header_exists()
checks if a header is present.
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 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: Thu, 12 Jan 2023 16:24:10 GMT
#> access-control-allow-origin: *
#> etag: W/"63c0342a-4cee"
#> expires: Fri, 13 Jan 2023 13:43:46 GMT
#> cache-control: max-age=600
#> content-encoding: gzip
#> x-proxy-cache: MISS
#> x-github-request-id: 0801:12D8:1260968:1837ADB:63C15DBA
#> accept-ranges: bytes
#> date: Fri, 13 Jan 2023 13:34:13 GMT
#> via: 1.1 varnish
#> age: 26
#> x-served-by: cache-dfw-kdfw8210075-DFW
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1673616854.641532,VS0,VE0
#> vary: Accept-Encoding
#> x-fastly-request-id: 3e8d94d48f51472702b0699f03e53dadf3354cdb
#> content-length: 5309
resp %>% resp_headers("x-")
#> <httr2_headers>
#> x-proxy-cache: MISS
#> x-github-request-id: 0801:12D8:1260968:1837ADB:63C15DBA
#> x-served-by: cache-dfw-kdfw8210075-DFW
#> x-cache: HIT
#> x-cache-hits: 4
#> x-timer: S1673616854.641532,VS0,VE0
#> x-fastly-request-id: 3e8d94d48f51472702b0699f03e53dadf3354cdb
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