Decode response body as utf-8 and then parse as json

This changes response parsing so that the response body is decoded as
UTF-8 and then parsed as JSON, rather than the other way around.

This fixes the handling of responses that contain Unicode characters
which are encoded with multiple bytes in UTF-8, such as emojis.
This commit is contained in:
algal 2023-03-30 17:39:58 -07:00 committed by karthink
parent a500c76a68
commit c2ad1c004d

View file

@ -417,16 +417,15 @@ INFO is a plist with the following keys:
(if-let* ((status (buffer-substring (line-beginning-position) (line-end-position))) (if-let* ((status (buffer-substring (line-beginning-position) (line-end-position)))
(json-object-type 'plist) (json-object-type 'plist)
(response (progn (forward-paragraph) (response (progn (forward-paragraph)
(let ((json-str (decode-coding-string
(buffer-substring-no-properties (point) (point-max))
'utf-8)))
(condition-case nil (condition-case nil
(json-read) (json-read-from-string json-str)
(json-readtable-error 'json-read-error))))) (json-readtable-error 'json-read-error))))))
(cond (cond
((string-match-p "200 OK" status) ((string-match-p "200 OK" status)
(list :content (string-trim (list :content (string-trim (map-nested-elt response '(:choices 0 :message :content)))
(decode-coding-string
(map-nested-elt
response '(:choices 0 :message :content))
'utf-8))
:status status)) :status status))
((plist-get response :error) ((plist-get response :error)
(let* ((error-plist (plist-get response :error)) (let* ((error-plist (plist-get response :error))