branch: elpa/gptel
commit 69259ca5174ef2ed21633cf8b8021d15095e56f8
Author: Karthik Chikmagalur <karthikchikmaga...@gmail.com>
Commit: Karthik Chikmagalur <karthikchikmaga...@gmail.com>

    gptel-curl: Pass payload unencoded to Curl process
    
    gptel fails on MS-windows 10/11 in Japanese environments (#1033)
    because the built-in Curl does not accept UTF-8 encoded
    arguments (and expects CP932).  Fix by decoding the JSON-encoded
    payload (which always uses UTF-8) to whatever is appropriate to
    the user's process-environment.  This fix was provided by
    @shingo256.
    
    Ensure that we don't needlessly create a copy of the payload (if
    it can be avoided) by setting the NOCOPY flag when running
    `decode-coding-string'.
    
    * gptel-curl.el (gptel-curl--get-args): Use `decode-coding-string'
    around the JSON-encoded payload.
    
    * gptel.el (gptel--url-get-response): Do NOT use
    `decode-coding-string'.  This change slipped in by mistake in
    90b41b1.  It is not required here since the string never leaves
    Emacs' environment.  Further, decoding the UTF-8 encoded JSON and
    attempting to send non-ASCII chars actually causes an encoding
    error.
---
 gptel-curl.el | 2 +-
 gptel.el      | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gptel-curl.el b/gptel-curl.el
index 45d178804ad..449e8a28a51 100644
--- a/gptel-curl.el
+++ b/gptel-curl.el
@@ -74,7 +74,7 @@ REQUEST-DATA is the data to send, TOKEN is a unique 
identifier."
                     (with-current-buffer (plist-get info :buffer)
                       (funcall backend-url))
                   backend-url)))
-         (data-json (encode-coding-string (gptel--json-encode data) 'utf-8))
+         (data-json (decode-coding-string (gptel--json-encode data) 'utf-8 t))
          (headers
           (append '(("Content-Type" . "application/json"))
                   (when-let* ((header (gptel-backend-header gptel-backend)))
diff --git a/gptel.el b/gptel.el
index 44e28a3659f..c15ca16717e 100644
--- a/gptel.el
+++ b/gptel.el
@@ -3203,10 +3203,10 @@ the response is inserted into the current buffer after 
point."
          (backend (plist-get info :backend))
          (callback (or (plist-get info :callback) ;if not the first run
                        #'gptel--insert-response)) ;default callback
+         ;; NOTE: We don't need the decode-coding-string dance here since we
+         ;; don't pass it to the OS environment and Curl.
          (url-request-data
-          (decode-coding-string
-           (gptel--json-encode (plist-get info :data))
-           'utf-8 t)))
+          (gptel--json-encode (plist-get info :data))))
     (when (with-current-buffer (plist-get info :buffer)
             (and (derived-mode-p 'org-mode)
                  gptel-org-convert-response))

Reply via email to