url encode support

Started by csfreebird, April 09, 2015, 09:26:28 PM

Previous topic - Next topic

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:
Quote
http://localhost/wind_tunnel/clusters/%E8%B7%9F%E8%B8%AA%E7%B3%BB%E7%BB%9F/hosts">http://localhost/wind_tunnel/clusters/跟踪系统/hosts


when using browser, this url will be encoded by browser automatically:
Quote
http://localhost/wind_tunnel/clusters/%E8%B7%9F%E8%B8%AA%E7%B3%BB%E7%BB%9F/hosts">http://localhost/wind_tunnel/clusters/% ... B%9F/hosts">http://localhost/wind_tunnel/clusters/%E8%B7%9F%E8%B8%AA%E7%B3%BB%E7%BB%9F/hosts


I didn't find url encode/decode functions in newlisp manual, does anybody implement this before?

rrq

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


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


(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

#2
see the 4th snippet on this page:



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