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:
parent
a500c76a68
commit
c2ad1c004d
1 changed files with 7 additions and 8 deletions
13
gptel.el
13
gptel.el
|
@ -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))
|
||||||
|
|
Loading…
Add table
Reference in a new issue