Convenience wrapper around a bunch of httr2 functions that returns
TRUE
if the specifiedurl
could be resolved and aHEAD
request could be successfully completed, orFALSE
in any other case.
Usage
is_http_success(
url,
max_tries = 1L,
retry_on_failure = FALSE,
is_transient = NULL,
verbosity = NULL
)
Arguments
- url
HTTP protocol address. The scheme is optional, so both
"google.com"
and"https://google.com"
will work. A character scalar.- max_tries
Maximum number of request attempts in case of an HTTP error. An integerish scalar. Retries are performed using exponential backoff and jitter, see
httr2::req_retry()
for details.- retry_on_failure
Treat low-level failures as if they are transient errors that can be retried.
- is_transient
A predicate function that takes a single argument (the response) and returns
TRUE
orFALSE
specifying whether or not the response represents a transient error.- verbosity
How much information to print? This is a wrapper around
req_verbose()
that uses an integer to control verbosity:0
: no output1
: show headers2
: show headers and bodies3
: show headers, bodies, and curl status messages.
Use
with_verbosity()
to control the verbosity of requests that you can't affect directly.
Details
This function is similar to RCurl::url.exists()
, i.e. it only retrieves the header, no body, but is based on httr2 which in turn is
based on curl.
For checks on lower levels of the network stack like performing DNS queries or TCP port pings, see the pingr package.
See also
Other HTTP functions:
http_get_cached()
,
is_url()
Examples
pal::is_http_success("goo.gl")
#> [1] FALSE
pal::is_http_success("https://google.com/")
#> [1] TRUE
pal::is_http_success("https://google.not/")
#> [1] FALSE
# by default, requests are only retried on HTTP 429 and 503 status codes
pal::is_http_success(url = "https://httpstat.us/503",
max_tries = 2,
verbosity = 1)
#> -> HEAD /503 HTTP/1.1
#> -> Host: httpstat.us
#> -> User-Agent: httr2/1.1.2 r-curl/6.2.2 libcurl/7.81.0
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip, br, zstd
#> ->
#> <- HTTP/1.1 503 Service Unavailable
#> <- Content-Length: 23
#> <- Content-Type: text/plain
#> <- Date: Fri, 02 May 2025 03:19:18 GMT
#> <- Server: Kestrel
#> <- Retry-After: 5
#> <- Set-Cookie: ARRAffinity=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;Secure;Domain=httpstat.us
#> <- Set-Cookie: ARRAffinitySameSite=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;SameSite=None;Secure;Domain=httpstat.us
#> <- Strict-Transport-Security: max-age=2592000
#> <- Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53
#> <-
#> Waiting 5s for retry backoff ■■■■■■
#> Waiting 5s for retry backoff ■■■■■■■■■■■■■■■■
#> Waiting 5s for retry backoff ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#> -> HEAD /503 HTTP/1.1
#> -> Host: httpstat.us
#> -> User-Agent: httr2/1.1.2 r-curl/6.2.2 libcurl/7.81.0
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip, br, zstd
#> ->
#> <- HTTP/1.1 503 Service Unavailable
#> <- Content-Length: 23
#> <- Content-Type: text/plain
#> <- Date: Fri, 02 May 2025 03:19:23 GMT
#> <- Server: Kestrel
#> <- Retry-After: 5
#> <- Set-Cookie: ARRAffinity=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;Secure;Domain=httpstat.us
#> <- Set-Cookie: ARRAffinitySameSite=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;SameSite=None;Secure;Domain=httpstat.us
#> <- Strict-Transport-Security: max-age=2592000
#> <- Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53
#> <-
#> [1] FALSE
pal::is_http_success(url = "https://httpstat.us/500",
max_tries = 2,
verbosity = 1)
#> -> HEAD /500 HTTP/1.1
#> -> Host: httpstat.us
#> -> User-Agent: httr2/1.1.2 r-curl/6.2.2 libcurl/7.81.0
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip, br, zstd
#> ->
#> <- HTTP/1.1 500 Internal Server Error
#> <- Content-Length: 25
#> <- Content-Type: text/plain
#> <- Date: Fri, 02 May 2025 03:19:23 GMT
#> <- Server: Kestrel
#> <- Set-Cookie: ARRAffinity=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;Secure;Domain=httpstat.us
#> <- Set-Cookie: ARRAffinitySameSite=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;SameSite=None;Secure;Domain=httpstat.us
#> <- Strict-Transport-Security: max-age=2592000
#> <- Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53
#> <-
#> [1] FALSE
# to retry on *all* failing status codes, set `is_transient` accordingly:
pal::is_http_success(url = "https://httpstat.us/500",
max_tries = 2,
is_transient = \(x) TRUE,
verbosity = 1)
#> -> HEAD /500 HTTP/1.1
#> -> Host: httpstat.us
#> -> User-Agent: httr2/1.1.2 r-curl/6.2.2 libcurl/7.81.0
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip, br, zstd
#> ->
#> <- HTTP/1.1 500 Internal Server Error
#> <- Content-Length: 25
#> <- Content-Type: text/plain
#> <- Date: Fri, 02 May 2025 03:19:23 GMT
#> <- Server: Kestrel
#> <- Set-Cookie: ARRAffinity=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;Secure;Domain=httpstat.us
#> <- Set-Cookie: ARRAffinitySameSite=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;SameSite=None;Secure;Domain=httpstat.us
#> <- Strict-Transport-Security: max-age=2592000
#> <- Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53
#> <-
#> Waiting 2s for retry backoff ■■■■■■■■■■■■■■■
#> Waiting 2s for retry backoff ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#> -> HEAD /500 HTTP/1.1
#> -> Host: httpstat.us
#> -> User-Agent: httr2/1.1.2 r-curl/6.2.2 libcurl/7.81.0
#> -> Accept: */*
#> -> Accept-Encoding: deflate, gzip, br, zstd
#> ->
#> <- HTTP/1.1 500 Internal Server Error
#> <- Content-Length: 25
#> <- Content-Type: text/plain
#> <- Date: Fri, 02 May 2025 03:19:26 GMT
#> <- Server: Kestrel
#> <- Set-Cookie: ARRAffinity=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;Secure;Domain=httpstat.us
#> <- Set-Cookie: ARRAffinitySameSite=0d764ef9424fab39459c0575d19254986797feb6fd09be54806ff535a3966a70;Path=/;HttpOnly;SameSite=None;Secure;Domain=httpstat.us
#> <- Strict-Transport-Security: max-age=2592000
#> <- Request-Context: appId=cid-v1:3548b0f5-7f75-492f-82bb-b6eb0e864e53
#> <-
#> [1] FALSE