I have a simple API that doesn't even display in my web browser.
https://data.police.uk/api/crimes-at-location?date=2017-02&location_id=884227
Others seem to have my issue (here and here), but as a newbie, I still don't fully understand what the issue is, especially in the context of R.
The first post seems to suggest that there's an issue with 'same origin policy', while in the second post, the issue seems to be the way the url is read in.
How can I diagnose the issue to figure out where the problem is, and in R, how could I actually parse such an API?
My current code is below, but it returns empty, just as in my browser:
api <- "https://data.police.uk/api/crimes-at-location?date=2017-02&location_id=884227"
r <- GET(api)
json_as_text <- content(r, "text")
json <- fromJSON(json_as_text)
df <- json %>% as_tibble()
df
CodePudding user response:
The problem is that their example code assumes dates that are not available.
Reading into their API docs, one endpoint is Availability:
r <- httr::GET("https://data.police.uk/api/crimes-street-dates")
r$status_code
# [1] 200 # good, this is normal
sapply(httr::content(r, type="application/json"), `[[`, "date")
# [1] "2021-11" "2021-10" "2021-09" "2021-08" "2021-07" "2021-06" "2021-05" "2021-04" "2021-03" "2021-02" "2021-01" "2020-12"
# [13] "2020-11" "2020-10" "2020-09" "2020-08" "2020-07" "2020-06" "2020-05" "2020-04" "2020-03" "2020-02" "2020-01" "2019-12"
# [25] "2019-11" "2019-10" "2019-09" "2019-08" "2019-07" "2019-06" "2019-05" "2019-04" "2019-03" "2019-02" "2019-01" "2018-12"
Perhaps they have "expired" old data, or for whatever reason they can no longer return it. If we adapt your initial query to be more recent than "2018-12" (inclusive), then we indeed see data:
r <- httr::GET("https://data.police.uk/api/crimes-at-location?date=2018-12&location_id=884227")
r$status_code
# [1] 200
str(httr::content(r, type="application/json"))
# List of 1
# $ :List of 9
# ..$ category : chr "shoplifting"
# ..$ location_type : chr "Force"
# ..$ location :List of 3
# .. ..$ latitude : chr "52.643950"
# .. ..$ street :List of 2
# .. .. ..$ id : int 884227
# .. .. ..$ name: chr "On or near Abbey Gate"
# .. ..$ longitude: chr "-1.143042"
# ..$ context : chr ""
# ..$ outcome_status :List of 2
# .. ..$ category: chr "Investigation complete; no suspect identified"
# .. ..$ date : chr "2018-12"
# ..$ persistent_id : chr "d2fc26387d5fee5763d675272385868fced641579788665dcadf37fa782c3278"
# ..$ id : int 70623600
# ..$ location_subtype: chr ""
# ..$ month : chr "2018-12"
To me, the 500 is a bit misleading: while I understand that the API internal code likely hit an error condition, it also seems likely that this error condition (no data found) could have resulted in something more meaningful, such as one of several other HTTP error codes, or perhaps just an empty return (json empty dictionary {}, or just nothing).
