1

This url works in the browser, providing some JSON data.

It worked from R until very recently, it now returns:

library(jsonlite)
fromJSON("https://api.worldbank.org/v2/country?format=json")

# Error in open.connection(con, "rb") : 
#   SSL certificate problem: certificate has expired

library(rvest)
read_html("https://api.worldbank.org/v2/country?format=json")

# Error in open.connection(con, "rb") : 
#   SSL certificate problem: certificate has expired

What I know so far

I am not sure if this is an issue on the API side, or somewhere in R?

  • There seems to be an analogous solution here, although any solution I use must not use browser automation (selenium), but instead must use either jsonlite or rvest
stevec
  • 41,291
  • 27
  • 223
  • 311
  • I can't replicate - both lines work fine. It doesn't seem to be the API. Maybe try a different connection method? Perhaps `read_html(url("https://api.worldbank.org/v2/country?format=json", method= "libcurl"))` – Allan Cameron Jun 11 '20 at 15:23
  • @AllanCameron Thanks Allan. Can you say which version of R you're using? I am on 4.0.1 – stevec Jun 11 '20 at 15:24
  • 1
    the PC I'm on at the moment is running an old version: 3.6.1 – Allan Cameron Jun 11 '20 at 15:26
  • Interesting. [This](https://stackoverflow.com/questions/62246994/how-do-i-resolve-this-rcurl-error-ssl-certificate-problem-certificate-has-exp) related question is also running R 4. But that may be coincidence. – stevec Jun 11 '20 at 15:30
  • Did you try different `method = ` to see if that fixed it Steve? – Allan Cameron Jun 11 '20 at 15:54
  • @AllanCameron I tried but get `Error in open.connection(x, "rb") : cannot open the connection to 'https://api.worldbank.org/v2/country?format=json'` – stevec Jun 11 '20 at 15:56
  • Possibly also related (the answer indicates that in that case it was a problem with the API): https://stackoverflow.com/a/57339291/5783745 – stevec Jun 11 '20 at 15:58

3 Answers3

1

For anyone else who is having a similar issue

The Cause

The website owner had an expired SSL certificate.

I was able to confirm this via this website:

enter image description here

(imperfect) Solution

Since I have no control over the url's SSL certificate, I simply changed all the urls I was using from https to http.

For example:

"https://api.worldbank.org/v2/country?format=json"

changes to

"http://api.worldbank.org/v2/country?format=json"
stevec
  • 41,291
  • 27
  • 223
  • 311
0

I actually have this issue too... Either way I cannot access it. I get the following error message (WDIcache() does not, of course, work either)

Error in file(con, "r") : cannot open the connection to 'http://api.worldbank.org/indicators?per_page=25000&format=json'
Kim Ida
  • 5
  • 3
  • I think World Bank decommissioned version 1 of their API. Checkout v2 of their API (I had the same problem 2 days ago) – stevec Jun 23 '20 at 16:05
  • [Here](https://github.com/stevecondylios/priceR/commit/7794a6bb3b7ff8706a884bd227146d5965315901)'s an example of the update I had to make to v2. Hope that helps – stevec Jun 23 '20 at 16:07
  • Try installing the package from github, closing rstudio entirely, opening it back up and trying it again. I can see the issue raised [here](https://github.com/vincentarelbundock/WDI/issues/35) - I am not sure if it's solved yet (some say it is, others not), but there's one way to know for sure. I Ran `WDIcache()` just now and it works for me. To install from github `devtools::install_github("vincentarelbundock/WDI")` – stevec Jun 23 '20 at 16:29
  • I can see the package is now up to date on CRAN. if you reinstall the package it should work now as of 1 minute ago :-) - https://github.com/vincentarelbundock/WDI/issues/35 good luck! – stevec Jun 23 '20 at 16:51
0

You'll have to set the ssl settings in R with

httr::set_config(config(ssl_verifypeer = FALSE, ssl_verifyhost = FALSE))
Peter
  • 343
  • 5
  • 17