ELisp: URL Percent Decode/Encode

By Xah Lee. Date: . Last updated: .

Percent Encode URL


URL percent encoded string. e.g. " ""%20"

(require 'url-util)

(url-hexify-string "Dürer" )
;; "D%C3%BCrer"

;; Chinese
(url-hexify-string "文本")
;; "%E6%96%87%E6%9C%AC"

;; it'll also encode puntuation chars
(url-hexify-string "'(),/\"" )
;; "%27%28%29%2C%2F%22"

Like url-hexify-string, but leave URL's protocol part and domain etc parts intact.

(require 'url-util)

(url-encode-url "http://example.com/i♥cats")
;; "http://example.com/i%E2%99%A5cats"

Percent Decode URL


Decode URL percent encoded string. Example: "%20"" "

To decode a hexified string, use url-unhex-string then decode-coding-string

(require 'url-util)

(let (aa bb cc)

  (setq aa "http://example.org/Dürer")

  (setq bb (url-hexify-string aa))

  (setq cc (url-unhex-string bb))
  ;; "http://example.org/D\303\274rer"

  (decode-coding-string cc 'utf-8)
  ;; "http://example.org/Dürer"

2014-04-27 thanks nns for help.

Emacs, Percent Encode/Decode Url