url encode support

Q&A's, tips, howto's
Locked
csfreebird
Posts: 107
Joined: Tue Jan 15, 2013 11:54 am
Location: China, Beijing
Contact:

url encode support

Post by csfreebird »

I want to use get-url to call one REST API, but the URL path of this REST API contains some Chiness words, like this:
when using browser, this url will be encoded by browser automatically:
I didn't find url encode/decode functions in newlisp manual, does anybody implement this before?

ralph.ronnquist
Posts: 228
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: url encode support

Post by ralph.ronnquist »

I suppose this is one of those skin-less cats, but these are what I'm currently using:

Code: Select all

(define (urlencode txt)
  (join (map (fn (c) (format "%02X" (char c))) (explode txt))))

Code: Select all

(define (urldecode txt)
  (replace "%(..)" (replace "+" (copy txt) " ") (char (int $1 32 16)) 0))
I have a memory of having searched some earlier discussion about this, and maybe these are from there. In any case, they work for me, for CGI scripts dealing with forms and form data.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: url encode support

Post by Lutz »

see the 4th snippet on this page:

http://www.newlisp.org/index.cgi?page=Code_Snippets

Locked