Session Ready
Exercise

Handling http failures

As mentioned, HTTP calls can go wrong. Handling that can be done with httr's http_error() function, which identifies whether a server response contains an error.

If the response does contain an error, calling http_error() over the response will produce TRUE; otherwise, FALSE. You can use this for really fine-grained control over results. For example, you could check whether the request contained an error, and (if so) issue a warning and re-try the request.

For now we'll try something a bit simpler - issuing a warning that something went wrong if http_error() returns TRUE, and printing the content if it doesn't.

Instructions
100 XP
  • Make a httr GET() request to the URL stored as fake_url, and store the result as request_result.
  • If http_error() returns TRUE, use warning() to issue the warning "The request failed".
  • If not, use content() (as demonstrated in previous exercises) to print the contents of the result.