Search code examples
apirequestelisp

Request.el "cannot create connection to api.stackexchange.com"


I'm using the request.el library (available via MELPA) to try and create a basic framework from which to start work in earnest on a Stack Exchange mode for Emacs. All I want to do is to be able to return the object as parsed by json-read to the calling function, but I can't even seem to make a connection.

I understand that to have my function return the object, the call must be made synchronously, so that is what :sync t is there for. I've considered making it an asynchronous call, but I don't think it would be beneficial considering its use-case.

At first, after I looked at the messages, I thought 'Maybe I don't have necessary binaries.' I did test request.el with some example calls that come with its documentation, and they work fine, so that's out.

I'm at a loss as to what is wrong. I don't yet have much experience, successful or otherwise, with anything dealing with a network, and don't fully understand the error message I'm getting. As far as I can tell, port 443 of the API is giving me the silent treatment, but I'm hesitant to think that's the case ;).

;; Works like a charm
(defun alist-to-json (alist)
  "Converts the key-value pairs of `ALIST` into a JSON-friendly
string: \"key1=value1&key2=value2&\"."
  (apply 'concat
     (mapcar (lambda (kv)
               (format "%s=%s&" (car kv)
                       (if (stringp (cdr kv)) 
                           (cdr kv)
                         (number-to-string (cdr kv)))))
             alist)))


(defvar stack-api-root "https://api.stackexchange.com/2.1/")

(require 'json)
(require 'request)

(defun stack-api-request (call keys-alist)
  "Makes the specified `CALL` to the Stack Exchange API with the
  key-value pairs given `KEYS-ALIST`.  For example,

  (stack-api-request \"sites\" '((page . 2) (page_size . 25)))"

  (let* ((base-call (concat stack-api-root call "?"))
         (options (alist-to-json keys-alist)))
    (request base-call
     :params options
     :parser 'json-read
     :sync t)))

Backtrace

Debugger entered--Lisp error: (error "Could not create connection to api.stackexchange.com:443")
  signal(error ("Could not create connection to api.stackexchange.com:443"))
  error("Could not create connection to %s:%d" "api.stackexchange.com" 443)
  url-http([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil))
  url-https([cl-struct-url "https" nil nil "api.stackexchange.com" nil "/2.1/sites?&" nil nil t nil t] #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil))
  url-retrieve-internal("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"] (nil) nil nil)
  url-retrieve("https://api.stackexchange.com/2.1/sites?&" #[128 "\302\303\304p#\210\300\305\240\210\301p\240\207" [(nil) (nil) url-debug retrieval "Synchronous fetching done (%S)" t] 5 "\n\n(fn &rest IGNORED)"])
  url-retrieve-synchronously("https://api.stackexchange.com/2.1/sites?&")
  request--url-retrieve-sync("https://api.stackexchange.com/2.1/sites?&" :params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response #0) nil nil nil url-retrieve nil])
  apply(request--url-retrieve-sync "https://api.stackexchange.com/2.1/sites?&" (:params "page=2&page_size=25&" :parser json-read :sync t :error (closure (t) (&rest args) (apply (quote request-default-error-callback) (quote "https://api.stackexchange.com/2.1/sites?") args)) :url "https://api.stackexchange.com/2.1/sites?&" :response [cl-struct-request-response nil nil nil nil nil "https://api.stackexchange.com/2.1/sites?&" nil #0 nil nil nil url-retrieve nil]))
  request("https://api.stackexchange.com/2.1/sites?" :params "page=2&page_size=25&" :parser json-read :sync t)
  (let* ((base-call (concat stack-api-root call "?")) (options (alist-to-json keys-alist))) (request base-call :params options :parser (quote json-read) :sync t))
  stack-api-request("sites" ((page . 2) (page_size . 25)))
  eval((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil)
  eval-expression((stack-api-request "sites" (quote ((page . 2) (page_size . 25)))) nil)
  call-interactively(eval-expression nil nil)

Messages:

Contacting host: api.stackexchange.com:443
Opening TLS connection to `api.stackexchange.com'...
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com'...failed
Opening TLS connection with `gnutls-cli --insecure -p 443 api.stackexchange.com --protocols ssl3'...failed
Opening TLS connection with `openssl s_client -connect api.stackexchange.com:443 -no_ssl2 -ign_eof'...failed
Opening TLS connection to `api.stackexchange.com'...failed

I checked to make sure that this was not a problem with cURL. The call I use for curl is

curl api.stackexchange.com/2.1/sites --compressed

From the outside looking into the request repository, request is doing this as well. I don't know what could be going wrong.


Solution

  • I boiled down your example to the following snippet to reproduce your problem but it actually worked. Can you try this one?

    (request
     "http://api.stackexchange.com/2.1/sites"
     :parser 'json-read
     :params '((page . "2") (page_size . "25"))
     :success (lambda (&rest args) (princ (plist-get args :data))))
    

    It should print some data to *Messages* buffer and echo area.

    EDIT: It seems that the problem in your example is that you are passing string to PARAMS which takes only alist. I will change code to raise error so that it is easier to debug.