Creating POST with elisp url package in emacs: utf-8 problem

I am currently creating a Rest client to create pastie.el blog posts. The main goal is to write text in emacs and make a message in a Rails application that will create it. It works fine until I type in either Spanish or Japanese, then I get a 500 error. By the way, pastie.el also has this problem.

Here is the code:

(require 'url)

(defun create-post() (interactive) (let ((url-request-method "POST") (url-request-extra-headers '(("Content-Type" . "application/xml"))) (url-request-data (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<post>" "<title>" "Not working with spanish nor japanese" "</title>" "<content>" ;; "日本語" ;; not working ;; "ñ" ;; not working either "h1. Textile title\n\n" "*Textile bold*" "</content>" "</post>")) ) ; end of let varlist (url-retrieve "http://127.0.0.1:3000/posts.xml" ;; CALLBACK (lambda (status) (switch-to-buffer (current-buffer))) ))) 

The only way I can imagine now that the problem can be fixed is that emacs encodes utf-8 characters, so that "ñ" becomes "& # 241" (which works, by the way).

What could be the problem for this problem?

EDIT: '*' is not equivalent to & ast; '. I meant that if I were encoded in utf-8 with emacs using, for example, 'sgml-char', the whole post would become utf-8. Like & ast; Textile bold & ast; making RedCloth unable to convert it to html. Sorry, this was very poorly explained.

+4
source share
2 answers

Guess: does it work if you set url-request-data to

 (encode-coding-string (concat "<?xml etc...") 'utf-8) 

instead

There is nothing to tell url which encoding system you use, so I think you should encode your data yourself. This should also give the correct Content-length header, since it comes only from (length url-request-data) , which obviously will give the wrong result for most UTF-8 lines.

+6
source

Thanks to @legoscia, I now know that I have to encode the data myself. I will post the function here for future reference:

 (require 'url) (defun create-post() (interactive) (let ((url-request-method "POST") (url-request-extra-headers '(("Content-Type" . "application/xml; charset=utf-8"))) (url-request-data (encode-coding-string (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<post>" "<title>" "Not working with spanish nor japanese" "</title>" "<content>" "日本語\n\n" ;; working!!! "ñ\n\n" ;; working !!! "h1. Textile title\n\n" "*Textile bold*" "</content>" "</post>") 'utf-8) ) ) ; end of let varlist (url-retrieve "http://127.0.0.1:3000/posts.xml" ;; CALLBACK (lambda (status) (switch-to-buffer (current-buffer)) )))) ;let 
+2
source

All Articles