简体   繁体   中英

Reading error status code in R and handling the exception

I am getting some data from a server in json or xml format , and the server is sending status code in header like 500,200,404 etc. So how can I get the error status code or the error and handle it in R. If I can get a simple sample code or any reference, it would work.

Or if there is some other way around that will also work.

If you are just looking to collect the status responses, you just need to inspect/parse what you get back.

library(httr)

GET("www.google.com")$status

# [1] 200

As a starting point for error handling... if you just want console warnings then you can use the function of the same name.

info_get <- GET("www.google.com")

if (info_get$status == 200) {

  warning(paste0("Response ", info_get$status, " received from target."))

}

# Warning message:
# Response 200 received from target. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM