2009-08-26 18 views
4

我目前正在爲pastie.el的精神創建一個Rest客戶端來製作博客文章。主要目標是讓我在emacs中編寫紡織品,並向Rails應用程序發佈帖子,以創建它。它工作正常,直到我用西班牙語或日語輸入任何內容,然後出現500錯誤。 pastie.el也有這個問題。在emacs中使用url elisp包創建一個POST:utf-8問題

下面是代碼:

(需要「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))) 
      ))) 

我現在這個問題可以固定可以想象的唯一方法是通過使emacs的編碼UTF-8字符,以便那'''變成'&#241'(順便說一下)。

什麼可以解決這個問題?

編輯:'*'不等於*'。我的意思是,如果我用emacs編碼爲UTF-8,例如'sgml-char',它會使整個文章變成utf-8編碼。像* Textile bold *因此使RedCloth無法將其轉換爲html。對不起,這是非常糟糕的解釋。

+0

Emacs版本? application/xml沒有標題? 最後,如果*和*在連接的另一端不等價,則說明您沒有使用XML。 – jrockway 2009-08-26 07:56:13

+0

哎。添加了xml頭。我正在使用Emacs 23.0。謝謝。 – wallyqs 2009-08-26 08:20:52

+0

當然關於*相當於*?我從來沒有見過它,它不是XML的五個強制實體之一。如果Emacs產生這個錯誤,我會說這是一個錯誤。 – legoscia 2009-08-26 18:24:38

回答

6

猜測:如果你設置url-request-data

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

,而不是它的工作原理?

沒有什麼可以告訴網址您使用的編碼系統,所以我想你必須自己編碼你的數據。這也應該給出一個正確的Content-length標題,因爲它只是來自(length url-request-data),這顯然會給大多數UTF-8字符串帶來錯誤的結果。

+0

tttttt謝謝!!!這是什麼。感謝分享知識! – wallyqs 2009-08-26 19:26:24

2

感謝@legoscia我現在知道我必須自己編碼數據。我會在這裏發佈這個函數供將來參考:

(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