Emacs Lisp: URL Percent Decode/Encode

By Xah Lee. Date: . Last updated: .

Percent Encode URL

url-hexify-string
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"
url-encode-url
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

url-unhex-string
Decode URL percent encoded string. Example: "%20"" "

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

(require 'url-util)

(setq x "http%3A%2F%2Fexample.org%2FD%C3%BCrer")

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

2014-04-27 thanks nns for help.

Emacs, Percent Encode/Decode Url