Nicolas Raoul Nicolas Raoul - 3 months ago 51
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)


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"