Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to set coding system for encoding/decoding #85

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,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-coding-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 @@ -63,6 +63,10 @@
"Directory to store data related to request.el."
:type 'directory)

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

(defcustom request-curl "curl"
"Executable for curl command."
:type 'string)
Expand Down Expand Up @@ -918,10 +922,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-coding-system))
(with-temp-file tempfile
(setq buffer-file-coding-system 'binary)
(set-buffer-multibyte nil)
(setq buffer-file-coding-system request-coding-system)
(if (eq request-coding-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 @@ -1041,7 +1046,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-coding-system request-coding-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-coding-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