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?

import groovyx.net.http.URIBuilder

def url = 'https://my.url.com/'
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}"

Output

Start Date = 2018-01-00T00:00:00+00:00
End Date = 2018-01-01T00:00:00+00:00
URI: https://my.url.com/?StartTime=2018-01-00T00%3A00%3A00%2B00%3A00&EndTime=2018-01-01T00%3A00%3A00%2B00%3A00

1 answer

  • answered 2018-02-13 07:46 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: http://www.faqs.org/rfcs/rfc1738.html

    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('&'))