Groovy string changes when used in URI addQueryParam

The following is part of a script to make a Rest query using HTTPbuilder. The aim is to make successive queries and increment the day each time. The value of the StartDate and EndDate string change when they are used in the uri.addQueryParam and the ':' character shows the %3A value instead. Can anyone identify why and how I get it to stay as the : character?


def url = ''
def uri = new URIBuilder(url)

int i1 = 0
int i2 = 1

iS = String.format("%02d", i1)
StartDate = '2018-01-'+ iS + 'T00:00:00+00:00'
iE = String.format("%02d", i2)
EndDate = '2018-01-'+ iE +'T00:00:00+00:00'

println"Start Date = $StartDate"
println"End Date = $EndDate"

uri.addQueryParam 'StartTime', "${StartDate}"
uri.addQueryParam 'EndTime', "${EndDate}"

URIstring = uri.toString()

println"URI: ${URIstring}"


Start Date = 2018-01-00T00:00:00+00:00
End Date = 2018-01-01T00:00:00+00:00

1 answer

  • answered 2018-02-13 02:57 Evgeny Smirnov

    URI builder automatically encode value because HTTP URL has its specification, and it states that practically all non-latin characters need to be encoded. RFC:

    But you can set raw values if you wish:

    // Change:
    //uri.addQueryParam 'StartTime', "${StartDate}"
    //uri.addQueryParam 'EndTime', "${EndDate}"
    // to
    uri.setRawQuery([StartTime: StartDate, EndTime: EndDate].collect().join('&'))