Session Ready
Exercise

Constructing queries (Part I)

As briefly discussed in the previous video, the actual API query (which tells the API what you want to do) tends to be in one of the two forms. The first is directory-based, where values are separated by / marks within the URL. The second is parameter-based, where all the values exist at the end of the URL and take the form of key=value.

Constructing directory-based URLs can be done via paste(), which takes an unlimited number of strings, along with a separator, as sep. So to construct http://swapi.co/api/vehicles/12 we'd call:

paste("http://swapi.co", "api", "vehicles", "12", sep = "/")

Let's do that now! We'll cover parameter-based URLs later. In the mean time we can play with SWAPI, mentioned above, which is an API chock full of star wars data. This time, rather than a vehicle, we'll look for a person.

Instructions
100 XP

httr is loaded in your workspace.

  • Construct a directory-based API URL to http://swapi.co/api, looking for person 1 in people.
  • Assign the URL to directory_url.
  • Use GET to make an API call with directory_url.