Skip to content

Commit

Permalink
Handles HTTP 404 returned for no matches
Browse files Browse the repository at this point in the history
Unfortunately the API also (currently, and erroneously) yields 404 for
other reasons than "not found", so this change confirms by checking the
error message from the server.

Fixes #151.
  • Loading branch information
hlapp committed Feb 2, 2021
1 parent fb3411a commit c3024d9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion R/pkb_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ get_json_data <- function(url, query,
else
res <- httr::POST(url, httr::accept_json(), httr::user_agent(ua()),
body = query, encode = "form")
# some endpoints return HTTP 404 for failure to find data, though some
# also (erroneously) do so for other reasons
contLen <- res$headers$`content-length`
if (res$status_code == 404 && (! is.null(contLen)) && contLen > 0) {
# confirm by checking the error message
err <- httr::content(res, as = "text")
if (endsWith(err, "resource could not be found.")) return(NULL)
}
stop_for_pk_status(res)
# some endpoints return zero content for failure to find data
contLen <- res$headers$`content-length`
if ((! is.null(contLen)) && contLen == 0) return(NULL)

# if content-type is application/json, httr:content() doesn't assume UTF-8
Expand Down

0 comments on commit c3024d9

Please sign in to comment.