Sorry to necro, but you can do this using httr, but names()
on a response object from httr::GET()
doesn't reveal them.
You can either use httr::GET()
or httr::HEAD()
, the difference being that GET()
will also retrieve the object at the URI. So HEAD()
is a more polite call to the server if you only want to check headers.
For example:
str(httr::HEAD("https://mcmap.net/q/1762090/-get-response-header"))
#> List of 10
#> $ url : chr "https://mcmap.net/q/1762090/-get-response-header"
#> $ status_code: int 200
#> $ headers :List of 19
#> ..$ connection : chr "keep-alive"
#> ..$ cache-control : chr "private"
#> ..$ content-type : chr "text/html; charset=utf-8"
#> ..$ content-encoding : chr "gzip"
#> ..$ strict-transport-security: chr "max-age=15552000"
#> ..$ x-frame-options : chr "SAMEORIGIN"
#> ..$ x-request-guid : chr "1ce6cb52-2fd2-43b4-ac62-a577e2554f8e"
#> ..$ feature-policy : chr "microphone 'none'; speaker 'none'"
#> ..$ content-security-policy : chr "upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com"
#> ..$ accept-ranges : chr "bytes"
#> ..$ date : chr "Fri, 15 Apr 2022 19:25:01 GMT"
#> ..$ via : chr "1.1 varnish"
#> ..$ x-served-by : chr "cache-lga21933-LGA"
#> ..$ x-cache : chr "MISS"
#> ..$ x-cache-hits : chr "0"
#> ..$ x-timer : chr "S1650050702.976391,VS0,VE11"
#> ..$ vary : chr "Accept-Encoding,Fastly-SSL"
#> ..$ x-dns-prefetch-control : chr "off"
#> ..$ set-cookie : chr "prov=86c2b2fb-5e39-9798-60ba-0f65eee27a7c; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly"
#> ..- attr(*, "class")= chr [1:2] "insensitive" "list"
#> $ all_headers:List of 1
#> ..$ :List of 3
#> .. ..$ status : int 200
#> .. ..$ version: chr "HTTP/1.1"
#> .. ..$ headers:List of 19
#> .. .. ..$ connection : chr "keep-alive"
#> .. .. ..$ cache-control : chr "private"
#> .. .. ..$ content-type : chr "text/html; charset=utf-8"
#> .. .. ..$ content-encoding : chr "gzip"
#> .. .. ..$ strict-transport-security: chr "max-age=15552000"
#> .. .. ..$ x-frame-options : chr "SAMEORIGIN"
#> .. .. ..$ x-request-guid : chr "1ce6cb52-2fd2-43b4-ac62-a577e2554f8e"
#> .. .. ..$ feature-policy : chr "microphone 'none'; speaker 'none'"
#> .. .. ..$ content-security-policy : chr "upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com"
#> .. .. ..$ accept-ranges : chr "bytes"
#> .. .. ..$ date : chr "Fri, 15 Apr 2022 19:25:01 GMT"
#> .. .. ..$ via : chr "1.1 varnish"
#> .. .. ..$ x-served-by : chr "cache-lga21933-LGA"
#> .. .. ..$ x-cache : chr "MISS"
#> .. .. ..$ x-cache-hits : chr "0"
#> .. .. ..$ x-timer : chr "S1650050702.976391,VS0,VE11"
#> .. .. ..$ vary : chr "Accept-Encoding,Fastly-SSL"
#> .. .. ..$ x-dns-prefetch-control : chr "off"
#> .. .. ..$ set-cookie : chr "prov=86c2b2fb-5e39-9798-60ba-0f65eee27a7c; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly"
#> .. .. ..- attr(*, "class")= chr [1:2] "insensitive" "list"
#> $ cookies :'data.frame': 1 obs. of 7 variables:
#> ..$ domain : chr "#HttpOnly_.stackoverflow.com"
#> ..$ flag : logi TRUE
#> ..$ path : chr "/"
#> ..$ secure : logi FALSE
#> ..$ expiration: POSIXct[1:1], format: "2054-12-31 18:00:00"
#> ..$ name : chr "prov"
#> ..$ value : chr "86c2b2fb-5e39-9798-60ba-0f65eee27a7c"
#> $ content : raw(0)
#> $ date : POSIXct[1:1], format: "2022-04-15 19:25:01"
#> $ times : Named num [1:6] 0 0.0471 0.1203 0.2368 0.2819 ...
#> ..- attr(*, "names")= chr [1:6] "redirect" "namelookup" "connect" "pretransfer" ...
#> $ request :List of 7
#> ..$ method : chr "HEAD"
#> ..$ url : chr "https://mcmap.net/q/1762090/-get-response-header"
#> ..$ headers : Named chr "application/json, text/xml, application/xml, */*"
#> .. ..- attr(*, "names")= chr "Accept"
#> ..$ fields : NULL
#> ..$ options :List of 3
#> .. ..$ useragent : chr "libcurl/7.64.1 r-curl/4.3 httr/1.4.2"
#> .. ..$ nobody : logi TRUE
#> .. ..$ customrequest: chr "HEAD"
#> ..$ auth_token: NULL
#> ..$ output : list()
#> .. ..- attr(*, "class")= chr [1:2] "write_memory" "write_function"
#> ..- attr(*, "class")= chr "request"
#> $ handle :Class 'curl_handle' <externalptr>
#> - attr(*, "class")= chr "response"
Created on 2022-04-15 by the reprex package (v2.0.0)