Skip to content

Commit

Permalink
Enable to set coding system for encoding/decoding
Browse files Browse the repository at this point in the history
Non-ascii strings in JSON data would be unicoded when POSTing curl with JSON data because
buffer-file-coding-system in data buffer is 'binary.

But to change this code is not good for web page whose encoding is other encoding, so
set encode in variable request-conding-system.

(seq request-conding-system 'utf-8)
  • Loading branch information
shigemk2 authored and titaniumbones committed Dec 2, 2018
1 parent b12f54a commit 1562f9c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ PUT JSON data:
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'json data)))))
PUT JSON data including non-ascii strings:

.. code:: emacs-lisp
(setq request-conding-system 'utf-8)
(request
"http://httpbin.org/put"
:type "PUT"
:data (json-encode '(("key" . "値1") ("key2" . "値2")))
:headers '(("Content-Type" . "application/json"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'json data)))))
Another PUT JSON example (nested JSON using alist structure, how to represent a boolean & how to selectively evaluate lisp):

.. code:: emacs-lisp
Expand Down
13 changes: 9 additions & 4 deletions request.el
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"Directory to store data related to request.el."
:type 'directory)

(defcustom request-conding-system 'binary
"coding-system for request."
:type 'symbol)

(defcustom request-curl "curl"
"Executable for curl command."
:type 'string)
Expand Down Expand Up @@ -923,10 +927,11 @@ Currently it is used only for testing.")
(let ((tempfile (request--make-temp-file)))
(push tempfile (request-response--tempfiles response))
(let ((file-coding-system-alist nil)
(coding-system-for-write 'binary))
(coding-system-for-write request-conding-system))
(with-temp-file tempfile
(setq buffer-file-coding-system 'binary)
(set-buffer-multibyte nil)
(setq buffer-file-coding-system request-conding-system)
(if (eq request-conding-system 'binary)
(set-buffer-multibyte nil))
(insert data)))
(list "--data-binary" (concat "@" (request-untrampify-filename tempfile)))))
(when type (list "--request" type))
Expand Down Expand Up @@ -1046,7 +1051,7 @@ removed from the buffer before it is shown to the parser function.
(request-log 'debug "Run: %s" (mapconcat 'identity command " "))
(setf (request-response--buffer response) buffer)
(process-put proc :request-response response)
(set-process-coding-system proc 'binary 'binary)
(set-process-coding-system proc request-conding-system request-conding-system)
(set-process-query-on-exit-flag proc nil)
(set-process-sentinel proc #'request--curl-callback)))

Expand Down
26 changes: 26 additions & 0 deletions tests/test-request.el
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ See also:
(should (equal (assoc-default "method" data) "POST"))
(should (equal (assoc-default "form" data) '(("" . ""))))))

(request-deftest request-simple-post-json ()
(request-testing-with-response-slots
(request-testing-sync "report/some-path"
:type "POST" :data "{\"a\": 1, \"b\": 2, \"c\": 3}"
:headers '(("Content-Type" . "application/json"))
:parser 'json-read)
(should (equal status-code 200))
(should (equal (assoc-default 'path data) "some-path"))
(should (equal (assoc-default 'method data) "POST"))
(should (equal (request-testing-sort-alist (assoc-default 'json data))
'((a . 1) (b . 2) (c . 3))))))

(request-deftest request-post-files/simple-buffer ()
:backends (curl)
(with-current-buffer (get-buffer-create " *request-test-temp*")
Expand Down Expand Up @@ -359,6 +371,20 @@ To check that, run test with:
(should (equal (request-testing-sort-alist (assoc-default 'json data))
'((a . 1) (b . 2) (c . 3))))))

(request-deftest request-simple-post-multibyte-json ()
:backends (curl)
(setq request-conding-system 'utf-8)
(request-testing-with-response-slots
(request-testing-sync "report/some-path"
:type "POST" :data "{\"\": \"\"}"
:headers '(("Content-Type" . "application/json"))
:parser 'json-read)
(should (equal status-code 200))
(should (equal (assoc-default 'path data) "some-path"))
(should (equal (assoc-default 'method data) "POST"))
(should (equal (request-testing-sort-alist (assoc-default 'json data))
'((鍵 . ""))))))


;;; DELETE

Expand Down

0 comments on commit 1562f9c

Please sign in to comment.