Elisp: URL Percent Decode/Encode

By Xah Lee. Date: . Last updated: .

Percent Encode URL

use packag “url-util”

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. e.g. "%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.

Percent Decode/Encode Commands

Emacs: URL Percent Decode/Encode

Elisp + JavaScript Solution

Here's a solution by calling a external node.js script. Elisp: Calling External Command to Decode URL Percent Encoding.

Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.