how do I append path to my existing url through URI builder

10.7k Views Asked by At
def uri = new UriBuilder('http://someurl.com/api')

I want to append /$contacId/PhoneNumber to the above uri .

How Do I handle this case

3

There are 3 best solutions below

0
On

One-Liner using UriBuilder:

import javax.ws.rs.core.UriBuilder

def contactId = 42

def uri = UriBuilder.fromUri("http://someurl.com/api").path("$contactId/PhoneNumber")

println uri

Output:

http://someurl.com/api/42/PhoneNumber

Test:

Copy-paste the code into https://groovy-playground.appspot.com/

0
On
https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/UriBuilder.html

def uri = new URIBuilder('http://someurl.com/api')
String appendPath = "/contact/$contactId/PhoneNumber"
        uri.path += appendPath;
0
On

use path method

def uri = new URIBuilder('http://someurl.com/api');
 uri.path("/$contacId/PhoneNumber");