unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Iñigo Serna" <inigoserna@gmx.com>
To: emacs-devel@gnu.org
Subject: url-retrieve "Multibyte text in HTTP request" error when passing UTF-8
Date: Sat, 13 Jun 2020 23:08:19 +0200	[thread overview]
Message-ID: <87pna2eksc.fsf@inigo.katxi.org> (raw)

Hi,

I'm developing a package that uses `url-retrieve` to connect to a HTTP server passsing a string query as a json object.

It mostly works, except when I need to pass a non-ASCII string, where it fails with "Multibyte text in HTTP request" error [1] raised in function `url-http-create-request` in url-http.el.
This happens even if I encode the query string with UTF-8.

I guess this is related to bug #23750, that in v25.1 times added a check to make this error happen.
If I remove that "fix" the code works without any problems.

I also tried emacs-request package [3], it works using curl backend and fails when using url-retrieve backend with the same error.

I tried emacs versions 26.3 (from Fedora 32), and last commits from 27.x and 28.x branches.
The whole function is at [4].


I reread several times the bug information and I can't fully understand the need of that check, IMO it corresponds to the program developer, not to the API.
Anyway, is there anything I could do? Any way to pass data as a UTF-8 string?


Thanks in advance,
Iñigo Serna


[1]
Debugger entered--Lisp error: (error "Multibyte text in HTTP request: POST /jsonrpc.js H...")
signal(error ("Multibyte text in HTTP request: POST /jsonrpc.js H..."))
error("Multibyte text in HTTP request: %s" "POST /jsonrpc.js HTTP/1.1\15\nMIME-Version: 1.0\15\nConn...")
url-http-create-request()
url-http(#s(url :type "http" :user nil :password nil :host "nas" :portspec 9002 :filename "/jsonrpc.js" :target nil :attributes nil :fullness t :silent t :use-cookies nil :asynchronous nil) #f(compiled-function (&rest ignored) #<bytecode -0x113049c86a7cc012>) (nil))
url-retrieve-internal("http://nas:9002/jsonrpc.js" #f(compiled-function (&rest ignored) #<bytecode -0x113049c86a7cc012>) (nil) t t)
url-retrieve("http://nas:9002/jsonrpc.js" #f(compiled-function (&rest ignored) #<bytecode -0x113049c86a7cc012>) nil t t)
url-retrieve-synchronously("http://nas:9002/jsonrpc.js" t t 1)


[2] https://debbugs.gnu.org/cgi/bugreport.cgi?bug=23750

[3] https://github.com/tkf/emacs-request

[4] 
(defun lms2--cmd (query &optional playerid)
  "Sync HTTP request QUERY to LMS server.  PLAYERID is optional.
QUERY is a string."
  (setq playerid (or playerid lms2--default-playerid ""))
  (let* ((json-array-type 'list)
         (url-request-method "POST")
         (url-request-extra-headers '(("Content-Type" . "application/json; charset=utf-8")))
         (url-request-data (json-encode-alist `[(method . "slim.request") (params . [,playerid ,(split-string query)])]))
         (response (with-current-buffer (url-retrieve-synchronously (concat lms2-url "/jsonrpc.js") t t 1)
                     (goto-char (point-min))
                     (re-search-forward "^$")
                     (delete-region (point) (point-min))
                     (decode-coding-string (buffer-string) 'utf-8))))
    (unless (string= "" response)
      (alist-get 'result (json-read-from-string response)))))

(lms2--cmd (encode-coding-string "artists 0 10 search:Björk" 'utf-8))



             reply	other threads:[~2020-06-13 21:08 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-13 21:08 Iñigo Serna [this message]
2020-06-13 22:12 ` url-retrieve "Multibyte text in HTTP request" error when passing UTF-8 Dmitry Gutov
2020-06-13 23:24   ` Iñigo Serna
2020-06-13 23:27     ` Dmitry Gutov
2020-06-14  0:20     ` Stefan Monnier
2020-06-14  8:33       ` Yuri Khan
2020-06-14 13:49         ` Stefan Monnier
2020-06-14 17:30           ` Yuri Khan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87pna2eksc.fsf@inigo.katxi.org \
    --to=inigoserna@gmx.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).