Home > other >  Loop through error when checking http status code
Loop through error when checking http status code

Time:01-26

I have a list of websites and my goal is to check each of their status codes. I have the following, where urls is a list of websites.

for (i in seq_along(urls)) {
  r <- GET(test[i])
  s <- status_code(r)
}

When I run the for loop, I get the message: Error in curl::curl_fetch_memory(url, handle = handle) : schannel: SNI or certificate check failed: SEC_E_WRONG_PRINCIPAL (0x80090322) - The target principal name is incorrect.

I am thinking of handling this with tryCatch but I am not sure of the syntax or whether to place the entire for loop in tryCatch.

CodePudding user response:

You for loop actually works fine.

# install.packages('httr')
library(httr)

urls <- c('https://stackoverflow.com/questions/70857408/loop-through-error-when-checking-http-status-code',
          'https://stackoverflow.com/')

s <- numeric(length(urls))
for (i in seq_along(urls)) {
  
  # if URLs are listed
  if(inherits(urls, 'list')) {
    r <- GET(urls[[i]])
  }
  # if URLs are concatenated
  if(inherits(urls, 'character')) {
    r <- GET(urls[i])
  }
  
  s[i] <- status_code(r)
}

#> s
#[1] 200 200

(Edit after comment of the OP) If you would like to try whether a certain GET works and, if not, ignore errors and proceed with the loop, you may use the following construct.

s <- numeric(length(urls))
for (i in seq_along(urls)) {

  # if URLs are listed
  if(inherits(urls, 'list')) {
    r <- try(GET(urls[[i]]))
    if(inherits(r, "try-error")) next
  }

  # if URLs are concatenated
  if(inherits(urls, 'character')) {
    r <- try(GET(urls[i]))
    if(inherits(r, "try-error")) next
  }
  
  s[i] <- status_code(r)
}

#> s
#[1] 200 200
  •  Tags:  
  • Related