Groovy UrlEncode and UrlDecode a map


/ Published in: Groovy
Save to your folder(s)

lpt


Copy this code and paste it in your HTML
  1. def urlEncodeMap( aMap ) { // e.g. given map [x:1, y2] returns x=1&y=2
  2. def encode = { URLEncoder.encode( "$it".toString() ) }
  3. return aMap.collect { encode(it.key) + '=' + encode(it.value) }.join('&')
  4. }
  5.  
  6. def urlDecodeToMap( aUrlEncodedStr ) {
  7. def result = [:]
  8. def decode = { URLDecoder.decode(it) }
  9. def ampSplit = aUrlEncodedStr.tokenize('&')
  10. ampSplit.each {
  11. def eqSplit = it.tokenize('=')
  12. result[ decode(eqSplit[0]) ] = decode(eqSplit[1])
  13. }
  14. return result;
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.