Nicolas Raoul Nicolas Raoul - 1 year ago 213
Groovy Question

How to encode URL in Groovy?

Is there a kind of URLEncode in Groovy?

I can't find any String → String URL encoding utility.


dehydrogenase (NADP+)

instead of
would also be acceptable, as some implementations do that)

Answer Source

You could use

In your example above, the brackets must be encoded too:

def toEncode = "dehydrogenase (NADP+)"
assert == "dehydrogenase+%28NADP%2B%29"

You could also add a method to string's metaclass:

String.metaClass.encodeURL = {

And simple call encodeURL() on any string:

def toEncode = "dehydrogenase (NADP+)"
assert toEncode.encodeURL() == "dehydrogenase+%28NADP%2B%29"