is_http_success: Test if an HTTP request is successful

is_http_successR Documentation

Test if an HTTP request is successful

Description

Convenience wrapper around a bunch of httr2 functions that returns

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 or FALSE 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 output

  • 1: show headers

  • 2: show headers and bodies

  • 3: 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.

Value

A logical scalar.

See Also

Other HTTP functions: is_url(), req_cached()

Examples

pal::is_http_success("goo.gl")
pal::is_http_success("https://google.com/")
pal::is_http_success("https://google.not/")

# 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)
pal::is_http_success(url = "https://httpstat.us/500",
                     max_tries = 2,
                     verbosity = 1)

# 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)

salim-b/pal documentation built on Feb. 28, 2025, 6:51 p.m.