Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

stop_for_status

Take action on http error.


Description

Converts http errors to R errors or warnings - these should always be used whenever you're creating requests inside a function, so that the user knows why a request has failed.

Usage

stop_for_status(x, task = NULL)

warn_for_status(x, task = NULL)

message_for_status(x, task = NULL)

Arguments

x

a response, or numeric http code (or other object with status_code method)

task

The text of the message: either NULL or a character vector. If non-NULL, the error message will finish with "Failed to task".

Value

If request was successful, the response (invisibly). Otherwise, raised a classed http error or warning, as generated by http_condition()

See Also

http_status() and http://en.wikipedia.org/wiki/Http_status_codes for more information on http status codes.

Other response methods: content(), http_error(), http_status(), response()

Examples

x <- GET("http://httpbin.org/status/200")
stop_for_status(x) # nothing happens
warn_for_status(x)
message_for_status(x)

x <- GET("http://httpbin.org/status/300")
## Not run: 
stop_for_status(x)

## End(Not run)
warn_for_status(x)
message_for_status(x)

x <- GET("http://httpbin.org/status/404")
## Not run: 
stop_for_status(x)

## End(Not run)
warn_for_status(x)
message_for_status(x)

# You can provide more information with the task argument
warn_for_status(x, "download spreadsheet")
message_for_status(x, "download spreadsheet")

httr

Tools for Working with URLs and HTTP

v1.4.2
MIT + file LICENSE
Authors
Hadley Wickham [aut, cre], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.