Get startedGet started for free

HTTP? httr! (2)

Web content does not limit itself to HTML pages and files stored on remote servers such as DataCamp's Amazon S3 instances. There are many other data formats out there. A very common one is JSON. This format is very often used by so-called Web APIs, interfaces to web servers with which you as a client can communicate to get or store information in more complicated ways.

You'll learn about Web APIs and JSON in the video and exercises that follow, but some experimentation never hurts, does it?

This exercise is part of the course

Intermediate Importing Data in R

View Course

Exercise instructions

  • Use GET() to get the url that has already been specified in the sample code. Store the response as resp.
  • Print resp. What is the content-type?
  • Use content() to get the content of resp. Set the as argument to "text". Simply print out the result. What do you see?
  • Use content() to get the content of resp, but this time do not specify a second argument. R figures out automatically that you're dealing with a JSON, and converts the JSON to a named R list.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# httr is already loaded

# Get the url
url <- "http://www.omdbapi.com/?apikey=72bc447a&t=Annie+Hall&y=&plot=short&r=json"


# Print resp


# Print content of resp as text


# Print content of resp
Edit and Run Code