Function: gptel--url-retrieve

gptel--url-retrieve is a natively compiled function defined in gptel-request.el.

Signature

(gptel--url-retrieve URL &key METHOD DATA HEADERS (CONTENT-TYPE "application/json"))

Documentation

Retrieve URL synchronously with METHOD, DATA and HEADERS.

CONTENT-TYPE (optional) is the MIME type of the request body, defaults to "application/json".

Source Code

;; Defined in /nix/store/q3r7g81pvghw7a523kjk2k2zih3pfk8z-emacs-packages-deps/share/emacs/site-lisp/elpa/gptel-20261002.545/gptel-request.el
;; TODO: Handle and return HTTP errors
(cl-defun gptel--url-retrieve (url &key method data headers
                                   (content-type "application/json"))
  "Retrieve URL synchronously with METHOD, DATA and HEADERS.

CONTENT-TYPE (optional) is the MIME type of the request body,
defaults to \"application/json\"."
  (declare (indent 1))
  (let ((url-request-method (if (eq method 'post) "POST" "GET"))
        (url-request-data
         (when (eq method 'post)
           (encode-coding-string
            (pcase content-type
              ("application/json" (gptel--json-encode data))
              (_ data))
            'utf-8)))
        (url-mime-accept-string "application/json")
        (url-request-extra-headers
         `(("content-type" . ,content-type) ,@headers)))
    (with-current-buffer (url-retrieve-synchronously url 'silent)
      (goto-char url-http-end-of-headers)
      (gptel--json-read))))