Search code examples
rhttrrjson

Why is httr not reading a response-class object?


I'm trying to update a project from March 2018. Previously, I had used

library("httr")
library("rjson")
api.url <- "http://api.tvmaze.com/lookup/shows?imdb=tt1325113"
response <- GET(api.url)
response.list <- fromJSON(content(response))

Previously, this returned a list containing the parsed json information that I used sapply to extract the relevant information from. Now, it's showing

Error in UseMethod("content", x) : 
  no applicable method for 'content' applied to an object of class "response"

There appears to be some kind of change in the httr package but I can't figure out what it is. Any ideas of what might be different and how to get around it?


Solution

  • You have to specify the as argument of content, the code below should do the trick.

    library("httr")
    library("rjson")
    api.url <- "http://api.tvmaze.com/lookup/shows?imdb=tt1325113"
    response <- GET(api.url)
    
    response.list <- 
      fromJSON(content(response, as = "text"))